简体   繁体   中英

Jquery checkbox.checked always returns false

$("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.

jQuery objects do not have a checked property. Replace

current_checkbox.checked

with

current_checkbox.prop('checked')

http://api.jquery.com/prop

您正在使用current_checkbox.checked,复选框没有任何“已检查”属性,但您可以使用$('#id')。is(':checked')

You're using a jQuery element, not a DOM element. Use:

if(current_checkbox.prop("checked")) {

} //just for Steve <3

current_row.find("input[name=checkbox]")

I'm fairly sure this returns a jQuery object, not the underlying checkbox element.

if(current_checkbox[0].checked)

Should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM