在我的网站上,我有一张门票列表。在该列表的顶部,我有这个复选框,可以检查所有复选框,在列表中:添加值输入 - jQuery
<input type="checkbox" id="checkall" class="checkall" name="checkall">
该复选框,检查所有其他复选框有以下的jQuery:
$(document).ready(function(){
$('#checkall').toggle(function(){
$('input:checkbox').attr('checked','checked');
$(this).val('uncheck all')
},function(){
$('input:checkbox').removeAttr('checked');
$(this).val('check all');
})
})
$('input:checkbox').click(function() {
$("#userInfo").toggle(!($('input:checkbox:checked').length == 0));
});
$('#close').click(function() {
$("#userInfo").hide();
$('input:checkbox').removeAttr('checked');
});
虽然,我也希望将所有选中的复选框值放入我的表单中。我使用此代码为:
function updateTextArea()
{
var allVals = [];
$('#c_b :checked').each(function(){
allVals.push($(this).val());
});
jQuery('#ticketIds').val(allVals);
}
$(function()
{
$('#c_b input').click(updateTextArea);
updateTextArea();
});
这里是我的表格:
<form method="post" name="edit">
<input type="hidden" id="ticketIds" name="ticketIds" value="">
<input type="submit" class="button gray" value="Okay, do it" name=""> <a href="#" id="close">Close</a></form>
我的问题是,每当我用“检查所有”复选框,没有值被添加到#ticketIds。只有当我自己检查每个复选框时,值才会被添加。
我该怎么办,所以这两种方式?
据透露,你可以使用'.prop('checked',val)'','val'是一个布尔值。 – ThiefMaster