Checkbox Click not in sync -Jquery
I have a table with checkbox and text input column. The last row in the checkbox column has a "Check All" link.
Upon Check All click:
1: Text must chan开发者_开发百科ge "Uncheck All"
2: Check the box above in the column and set textbox text = 500.
Upon "Uncheck All": 1: Text must change "Check All" 2: Uncheck the box and clear the textbox.
But point 2 above is not working properly.
Steps:
1: Click "Check All" - Text changes to "Uncheck All" and box is checked but it does not set the Textbox to 500.
2: Click "Uncheck All" - Text Changes to "Check All" and box is unchecked but now it sets the textbox to 500.
I think it is some event handling issue but I haven't figured it out so far.
Any ideas what's going on?
Note: I will have to use "onclick" because my actual code is .aspx page which doesn't recognize onchange with checkbox.
Updated code Sample.
The function onClick() and setValue() runs before click() applies a check on to the checkbox; Therefore at step 1, it does not see the check box checked.
changed 'checked' to be boolean and is fine.
updated http://jsbin.com/inocud/16
class approach - http://jsbin.com/inocud/22
Try this
<script>
$(function () {
$(".checkall").click(function () {
if ($(this).text() == "Check All") {
$(this).text("Uncheck All");
$(this).parent().parent().prevUntil('tbody').each(function () {
$(this).find("td input:checkbox").attr('checked', 'checked').click();
});
}
else {
$(this).text("Check All");
$(this).parent().parent().prevUntil('tbody').each(function () {
$(this).find("td input:checkbox").removeAttr('checked').click();
});
}
});
});
function setValue(chb, txtid, valu) {
if ($(chb).is(":checked")) {
$("#" + txtid).val(valu);
}
else {
$("#" + txtid).val("");
}
}
</script>
Attempted a shot.
Updated (Its a hack but works): http://jsfiddle.net/2YGYt/
Another method (non hacky way): http://jsfiddle.net/2YGYt/3/
精彩评论