我需要你的建议。我在表格中有一个复选框。如果我想检查复选框,会附加一些内容并且有效。但是当我取消选中复选框时,我想要发生的事情就不起作用了。
这是我的jquery代码:
$('.table tbody').on('click','.detailscheck', function(){
$('.detailscheck').filter(':checked').each(function() {
var po_no = $(this).closest('tr').find(".pono").val();
var itemid = $(this).closest('tr').find(".item_id").val();
if(this.checked) {
$('div.finaldata').append('<tr> \
<td><input type="text" class="pono" id="po_no" name="po_no[]" value="'+po_no+'"> \
<input type="text" id="item_id" class="item_id" name="item_id[]" value="'+itemid+'"></td> \
</tr>');
}
else {
$('div.finaldata').append('<tr> \
<td><input type="text" class="pono" id="po_no" name="po_no[]" value="'+po_no+'"> \
<input type="text" id="item_id" class="item_id" name="item_id[]" value="'+itemid+'"></td> \
</tr>').remove();
}
});
})
答案 0 :(得分:0)
您只会循环遍历:checked
个项目,而是尝试遍历所有复选框,如下所示:
$('.detailscheck').filter(':checkbox');
如果$('.detailscheck')
是一个复选框,那么只需通过删除过滤器函数来循环遍历其元素:
$('.detailscheck').each(function(){...});
答案 1 :(得分:0)
试试这个,
而不是使用if(this.checked)
试试这样的事情..
$(function() {
$('#check').click(function() {
if($(':checkbox').prop('checked') == true)
{
alert("hi");
}
else if($(':checkbox').prop('checked') == false)
{
alert("hello");
}
});
});
检查这个小提琴 - &gt; http://jsfiddle.net/WnbNC/421/
答案 2 :(得分:0)
它将删除所有未经检查的值并保留已检查的值。 您也可以使用empty()来避免重复。
不是我的代码,我刚刚找到它。
$('#test').click(function() {
$('#out').text('');
$('#rowclick2 tr').filter(':has(:checkbox:checked)').each(function() {
// this = tr
$tr = $(this);
//get row values
$('#out').append(this.id);
});
})