Add values to input - jQuery
On my website, I have a list of tickets. In top of that list, I have this checkbox, that can check ALL checkboxes in the list:
<input type="checkbox" id="checkall" class="checkall" name="checkall">
This checkbox, checks all other checkboxes with the following 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');
});
Although, I also wish to have all the selected checkbox values put into my form. I use this code for that:
function updateTextArea()
{
var allVals = [];
$('#c_b :checked').each(function(){
allVals.push($(this).val());
});
jQuery('#ticketIds').val(allVals);
}
$(function()
{
$('#c_b input').click(updateTextArea);
updateTextArea();
});
And here is my form:
<form method="post" name="edit">
<input type="hidde开发者_JS百科n" id="ticketIds" name="ticketIds" value="">
<input type="submit" class="button gray" value="Okay, do it" name=""> <a href="#" id="close">Close</a></form>
My problem is, whenever I use the "check all" checkbox, NO values is added to the #ticketIds. ONLY when I check each checkbox myself, the values are added.
How can I do, so it works both ways?
Try calling your function whenever you click the check/uncheck all checkbox:
$(document).ready(function(){
$('#checkall').toggle(function(){
$('input:checkbox').attr('checked','checked');
$(this).val('uncheck all')
updateTextArea();
},function(){
$('input:checkbox').removeAttr('checked');
$(this).val('check all');
updateTextArea();
})
})
Not sure I understand what you are trying to accomplish, but this might help:
$('#checkall').toggle(function(){
$('input:checkbox').attr('checked','checked');
$('input:checkbox').trigger('click'); // add this line
$(this).val('uncheck all')
},function(){
//...
精彩评论