【问题标题】:Using jQuery how can I get the value of checkboxes? [duplicate]使用 jQuery 如何获取复选框的值? [复制]
【发布时间】:2013-01-11 03:23:55
【问题描述】:

可能重复:
How to retrieve checkboxes values in jQuery

我有以下表格:

<form action="index.php">
    <div class="answer"><input type="radio" name="vote_answers" value="3615736&1047558" class="vote_answers"> 1</div>

    <div class="answer"><input type="radio" name="vote_answers" value="3615736&1121626" class="vote_answers"> 2</div>

    <div class="answer"><input type="radio" name="vote_answers" value="3615736&9910782" class="vote_answers"> 3</div>

    <input type="submit" name="submit" id="button_submit_vote" value="submit">
</form>

当我点击提交时,我需要获取选中复选框的值

这是我的代码:

$('#button_submit_vote').on('click',function() {
    if($('.vote_answers').is(':checked')){var id=$(this).val();}
    alert(id);
});

我得到值“提交”,而不是复选框值。

【问题讨论】:

  • this 在这种情况下应该指向表单提交按钮,或者更准确地说 this = $('#button_submit_vote')。 $('.vote_answers').val() 可能就是你要找的东西

标签: jquery checkbox


【解决方案1】:

那是因为this指的是button_submit_vote元素,你可以:checked选择器:

var id = $('.vote_answers:checked').val();

【讨论】:

  • @LeoLex 如果这回答了您的问题,请使用左侧的复选标记进行标记。
  • 当然)我不能因为这需要时间。谢谢)
【解决方案2】:

你还没有走那么远:
$('.vote_answers:checked').val()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-19
    • 2018-08-25
    • 2012-12-20
    • 1970-01-01
    • 2020-06-01
    • 2012-07-02
    • 2013-01-06
    相关资源
    最近更新 更多