【问题标题】:How to see which radio buttons are clicked Jquery如何查看单击了哪些单选按钮 Jquery
【发布时间】:2012-01-05 01:16:40
【问题描述】:

我的页面上有一些单选按钮可供用户标记(如下)。除了单选按钮之外,还有一个带有 textarea 的“其他”按钮,用于以不同的原因写入。我希望能够查看通过 jquery 提交表单时按下了哪些按钮。我知道如何使用 .post() 但我不确定是否应该在单选按钮的每个 id 上使用 .val() 。或者,是否有一种检查可以检查整个组并获得检查的标签?如果按下另一个按钮,我很确定我可以在 textarea 上使用 .val()。

HTML:

<input type='radio' id='spam' name='flagGroup'></input><label for='spam'>It is spam</label><br>
<input type='radio' id='offTopic' name='flagGroup'></input><label for='offTopic'>It is off topic</label><br>
<input type='radio' id='quality' name='flagGroup'></input><label for='quality'>It does not meet quality standards</label><br>
<input type='radio' id='wrongLocation' name='flagGroup'></input><label for='wrongLocation'>It is in the wrong location</label><br>
<input type='radio' id='duplicate' name='flagGroup'></input><label for='duplicate'>There is a duplicate question</label><br>
<input type='radio' id='other' name='flagGroup'></input><label for='other'>Other:</label><br>
<textarea id='flagOtherTextarea' rows='2' cols='40' wrap='soft'></textarea>
<hr />
<input type='radio' id='mod'></input><label for='mod'>It needs moderator attention</label> 

【问题讨论】:

    标签: jquery html radio-button


    【解决方案1】:

    试试下面的选择器

    $allChecked = $('input[type="radio"]:checked');
    

    如果您想在输入后获取所有标签的文本,您可以执行以下操作

    $('input[type="radio"]:checked').each(function() {
      var label = $(this).next('label');
      alert(label.text());
    });
    

    【讨论】:

    • 谢谢,但这会给我单选按钮的标签值还是告诉我哪些已被选中?
    • @user802370 这将返回检查的input 元素集。然后,您可以进一步挖掘这些元素并获取标签,例如
    • 我对您的变量使用了警报并尝试了代码并返回“[object Object]”。
    • @user802370 那是因为它是一个选择器。如果您想查看个别对象,请使用each。我将更新我的示例以打印出所有值的标签文本。
    【解决方案2】:

    获取选中的单选按钮。但是您必须为每个分配值才能区分它们

       $('input[name=flagGroup]:checked').val()
    

    或者如果你想用于属性使用

       $('input[name=flagGroup]:checked').attr('for');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      • 1970-01-01
      相关资源
      最近更新 更多