質問

Trying to bind an mvc helper checkbox with jquery to a javascript function and that function will do something based on the fact that it is checked or not.

PART 1: How do you successfully bind the checkbox to a click or change event?

PART 2: How do you check the checkbox for checked or not?

<div class="editor-label">
    @Html.LabelFor(model => model.Immediate)
</div>
<div class="editor-field">
    @Html.CheckBoxFor(model => model.Immediate, new {id = "chkImmediate" })
</div>

My attempt at binding:

$("#chkImmediate").click(clickEvent());

function clickEvent() {
        alert($("#chkImmediate").checked); //want it to show true/false
}
役に立ちましたか?

解決

Assumming the id of the checkbox is correct, you can bind the event using jQuery like this:

$("#chkImmediate").change(function() {
    if(this.checked) {
        alert('It's checked!!');
    } else {
        alert('It's not checked!!');
    }
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top