checkbox is checked from the code but jquery cant see it?
Im working with asp.net mvc 2 and in my action i put a property to true by default so it will be checked when it writes in the code, its a checkbox.
this generates the following code
<input id="BookTalent_BookATalent" class="bookTalent" type="checkbox" value="true" name="BookTalent.BookATalent" checked="checked">
now i got a second checkbox "filmPermit" that is unchecked by default, now the way it supose to work is that if both checkboxes are checked nothing changes but if this "filmPermit" checkbox is checked by itself it supose to hide a div.
now the code i got works fine when i dont set the default check on the booktalent checkbox, like if i check the filmpermit checkbox it hide the div, and if the booktalent checkbox is checked it wont hide it. but why does it not work when i set the default to be true "checked" on booktalent checkbox?
this is my jquery for the checking of
$('.filmPermit').click(function () {
$('#filmPermit').toggle(this.checked);
if (this.checked) {
if ($('.bookTalent').is(':checked')) {
$('#lookingFor').toggle(true);
} else {
$(开发者_如何转开发'#lookingFor').toggle(false);
}
if ($('.quote').is(':checked')) {
$('#lookingFor').toggle(true);
} else {
$('#lookingFor').toggle(false);
}
} else {
$('#lookingFor').toggle(true);
}
}).triggerHandler('click');
EDIT: this is the code i use for markup
<%: Html.CheckBoxFor(model => model.BookTalent.BookATalent, new { @class = "bookTalent"}) %><label>Book A Talent</label>
<%: Html.CheckBoxFor(model => model.BookTalent.Quote, new { @class = "quote"}) %><label>Get Quote</label>
<%: Html.CheckBoxFor(model => model.BookTalent.FilmPermit, new { @class = "filmPermit" }) %><label>Film Permit</label>
The way this code is structured, the state of .bookTalent
is completely irrelevant. Immediately after checking .bookTalent
you have the following:
if ($('.quote').is(':checked')) {
$('#lookingFor').toggle(true);
} else {
$('#lookingFor').toggle(false);
}
which overrides whatever was done as a result of the check on .bookTalent
. I can't tell you how to fix it because you didn't say anything about what .quote
is supposed to do.
Here's my working theory.
You are checking $(".bookTalent").is(':checked')
instead of $("#BookTalent_BookATalent").is(':checked')
Selecting on the class could return multiple items and the .is() command will return whether ANY of the selected items are checked.
Try using the more precise format and see if it makes a difference.
BTW: You also have the same problem with $('.quote').is(':checked')
精彩评论