문제

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