javascript Jquery checkbox.checked 总是返回 false
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21120532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery checkbox.checked always returns false
提问by Bishal Paudel
$("tbody input[name='rooms_to_book']").on('change',function(){
var current_row = $(this).closest("tr");
var current_checkbox = current_row.find("input[name=checkbox]");
if(current_checkbox.checked) {
var price_per_night = parseFloat(current_row.find("td[name=price]").text().replace('$',''));
var rooms_to_book = parseInt(current_row.find("input[name=rooms_to_book]").val());
rooms_to_book = rooms_to_book ? rooms_to_book : 0;
total_price = price_per_night * rooms_to_book;
}else{
total_price = 0;
}
current_row.find("td[name=total_price]").text('$'+ total_price);
});
The current_checkbox.checked
always returns false, the html is correct.
将current_checkbox.checked
始终返回false,则HTML是正确的。
回答by Blazemonger
jQuery objects do not have a checked
property. Replace
jQuery 对象没有checked
属性。代替
current_checkbox.checked
with
和
current_checkbox.prop('checked')
回答by Kerem Kulak
You are using current_checkbox.checked, checkbox has not any 'checked' properties, but you can use $('#id').is(':checked')
您正在使用 current_checkbox.checked,复选框没有任何 'checked' 属性,但您可以使用 $('#id').is(':checked')
回答by Sterling Archer
You're using a jQuery element, not a DOM element. Use:
您使用的是 jQuery 元素,而不是 DOM 元素。利用:
if(current_checkbox.prop("checked")) {
} //just for Steve <3
回答by Niet the Dark Absol
current_row.find("input[name=checkbox]")
current_row.find("input[name=checkbox]")
I'm fairly sure this returns a jQuery object, notthe underlying checkbox element.
我很确定这会返回一个 jQuery 对象,而不是底层的复选框元素。
if(current_checkbox[0].checked)
Should work.
应该管用。