最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery checkbox.checked always returns false - Stack Overflow

programmeradmin2浏览0评论
$("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.

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

Share Improve this question edited Jan 14, 2014 at 17:55 j08691 208k32 gold badges268 silver badges280 bronze badges asked Jan 14, 2014 at 17:54 Bishal PaudelBishal Paudel 1,9462 gold badges21 silver badges30 bronze badges 4
  • 5 The HTML may be correct but you need to add it to your question. – j08691 Commented Jan 14, 2014 at 17:55
  • check this out might help you api.jquery.com/attr – Charles380 Commented Jan 14, 2014 at 17:56
  • 1 checked isn't a property of a jQuery object – crush Commented Jan 14, 2014 at 17:57
  • possible duplicate of JS .checked vs jquery attr('checked'), what is the difference? – Felix Kling Commented Jan 14, 2014 at 17:59
Add a comment  | 

4 Answers 4

Reset to default 11

jQuery objects do not have a checked property. Replace

current_checkbox.checked

with

current_checkbox.prop('checked')

http://api.jquery.com/prop

You are using current_checkbox.checked, checkbox has not any 'checked' properties, but you can use $('#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.

发布评论

评论列表(0)

  1. 暂无评论