2014-01-14 60 views
4
$("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); 
}); 

current_checkbox.checked总是返回false,html是正确的。jquery checkbox.checked总是返回false

+5

的HTML可能是正确的,但你需要将其添加到您的问题。 – j08691

+0

检查这可能会帮助你http://api.jquery.com/attr/ – Charles380

+1

'checked'不是一个jQuery对象的属性 – crush

回答

2

您使用jQuery的元素,而不是一个DOM元素。用途:

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

} //just for Steve <3 
1

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

我相当肯定,这将返回一个jQuery对象,底层的复选框元素。

if(current_checkbox[0].checked) 

应该工作。 。

4

您使用current_checkbox.checked,复选框并没有任何“检查”的属性,但你可以用$(“#ID”)是(“:检查”)