jQuery:change事件中未定义checked属性
我看到很多关于这个话题的问题,但没有一个能解决我的问题 问题是:我有一个复选框,当用户单击它时需要checked属性的新值 我尝试过不同的事件附加程序,比如on.(“change”..和.change(function()…但它总是返回“undefined”。 我还测试了(带有警报)变量的赋值是否不止一次,但它们没有,只有一次 请考虑脚本,告诉我,如果你知道我做错了什么,或者我应该发布更多的信息。jQuery:change事件中未定义checked属性,jquery,checkbox,onchange,Jquery,Checkbox,Onchange,我看到很多关于这个话题的问题,但没有一个能解决我的问题 问题是:我有一个复选框,当用户单击它时需要checked属性的新值 我尝试过不同的事件附加程序,比如on.(“change”..和.change(function()…但它总是返回“undefined”。 我还测试了(带有警报)变量的赋值是否不止一次,但它们没有,只有一次 请考虑脚本,告诉我,如果你知道我做错了什么,或者我应该发布更多的信息。 $(document).ready(function () { $('#invoi
$(document).ready(function () {
$('#invoice_list :checkbox').on('change', function () {
var _invoice_id;
var _payed;
var _sent;
$(this).parent().siblings().each(function () {
if ($(this).hasClass('name?ajax_invoice_id')) {
_invoice_id = $(this).text();
}
if ($(this).hasClass('name?ajax_payed')) {
_payed = $(this).children(':checkbox').prop('checked');
}
if ($(this).hasClass('name?ajax_sent')) {
_sent = $(this).children(':checkbox').prop('checked');
}
});
alert('invoice_id: ' + _invoice_id + '\r\npayed: ' + _payed + '\r\nsent: ' + _sent);
});
});
如果我选中“payed”复选框,则无论新值是多少,这都是结果(与“sent”相同):
85
31-10-2015 16:54:27
31-10-2015 00:00:00
31-10-2015 00:00:00
4800,00
6000,00
1200,00
25,00 %
1.
6,00
800,00
36
25-10-2015 18:54:35
25-10-2015 00:00:00
25-10-2015 00:00:00
6400,00
8000,00
1600,00
25,00 %
1.
8,00
800,00
检查下面的解决方案将解决您的问题,并确保兄弟姐妹的课程符合您的期望
$(document).ready(function () {
$("input[type='checkbox']").on('change', function () {
var $parent = $(this).parent("td");
var _invoice_id= $parent.siblings("td.name\\?ajax_invoice_id").html();
var _payed="";
if ($parent.hasClass("name?ajax_payed")) {
_payed = this.checked;
} else {
_payed = $parent.siblings("td.name\\?ajax_payed").find("input[type='checkbox']").is(":checked");
}
var _sent="";
if ($parent.hasClass("name?ajax_sent")) {
_sent = this.checked;
} else {
_sent = $parent.siblings("td.name\\?ajax_sent").find("input[type='checkbox']").is(":checked");
}
alert('invoice_id: ' + _invoice_id + '\r\npayed: ' + _payed + '\r\nsent: ' + _sent);
});
});
已更新
$('table input[type=checkbox]').on('change', function() {
var _invoice_id = 'not found',
_payed = 'not found',
_sent = 'not found',
$this = $(this);
$this.closest('tr').children('td').each(function() {
var $this = $(this);
if ($this.hasClass('name?ajax_invoice_id')) {
_invoice_id = $(this).text();
}
if ($this.hasClass('name?ajax_payed')) {
_payed = $(this).children('input[type=checkbox]').prop('checked');
}
if ($this.hasClass('name?ajax_sent')) {
_sent = $(this).children('input[type=checkbox]').prop('checked');
}
});
alert('invoice_id: ' + _invoice_id + '\r\npayed: ' + _payed + '\r\nsent: ' + _sent);
});
为了解释您的实际问题,
$(this).parent().sibles()
只将其同级返回到集合,而不是包含您单击的复选框的td
您可以使用.addBack()
,它将包括兄弟姐妹+本身:-
$(文档).ready(函数(){
$(“#发票列表:复选框”)。在('change',function()上{
var _发票_id;
已支付的风险价值;
var_发送;
$(this).parent().sibbins().addBack().each(function()){
if($(this).hasClass('name?ajax\u invoice\u id')){
_发票id=$(this).text();
}
if($(this).hasClass('name?ajax\u payed')){
_payed=$(this).children(':checkbox').prop('checked');
}
if($(this).hasClass('name?ajax_sent')){
_sent=$(this).children(':checkbox').prop('checked');
}
});
警报(“发票id:”+“发票id+”\r\n显示:“+”已付款+”\r\n发送:“+”;
});
});
85
31-10-2015 16:54:27
31-10-2015 00:00:00
31-10-2015 00:00:00
4800,00
6000,00
1200,00
25,00 %
1.
6,00
800,00
36
25-10-2015 18:54:35
25-10-2015 00:00:00
25-10-2015 00:00:00
6400,00
8000,00
1600,00
25,00 %
1.
8,00
800,00
将$(“#发票列表:复选框”)
更改为$(“#发票列表输入[type=checkbox])
@MehdiDehghani刚刚尝试过,但仍然是相同的消息框:-/请显示html
@MehdiDehghani我已经发布了html的屏幕打印。将所有:checkbox
替换为输入[type=checkbox]
,然后告诉我这个值的未定义的,\u payed,\u sentI我复制了你的脚本,但它还是一样-在我单击的复选框上未定义。嗯,我不明白-仍然未定义..我尝试了ie、ff和chrome。它在小提琴上对你有效?是的,它对我有效…你没有定义吗在JSFIDLE中?是的。什么该死的…哦,我发现我可以使用eventarg和目标值。无论如何谢谢你的帮助,尽管我们不想迭代兄弟姐妹,检查这个解决方案谢谢Mehdi,但你的小提琴只是给了我“未找到”在我单击的一个答案中,我总是发现了解问题发生的原因很有用,而不仅仅是找到不同的解决方案。另一个答案是转到行,然后获取其所有单元格。因此,通过传递使用同级的问题。两个答案都有效:)
$('table input[type=checkbox]').on('change', function() {
var _invoice_id = 'not found',
_payed = 'not found',
_sent = 'not found',
$this = $(this);
$this.closest('tr').children('td').each(function() {
var $this = $(this);
if ($this.hasClass('name?ajax_invoice_id')) {
_invoice_id = $(this).text();
}
if ($this.hasClass('name?ajax_payed')) {
_payed = $(this).children('input[type=checkbox]').prop('checked');
}
if ($this.hasClass('name?ajax_sent')) {
_sent = $(this).children('input[type=checkbox]').prop('checked');
}
});
alert('invoice_id: ' + _invoice_id + '\r\npayed: ' + _payed + '\r\nsent: ' + _sent);
});