【问题标题】:How to get checked radio buttons value in jQuery mobile?如何在 jQuery mobile 中获取选中的单选按钮值?
【发布时间】:2012-11-30 17:20:20
【问题描述】:

我有一组单选按钮组。我怎样才能得到每个组的检查是或现在并将它们添加到数组中?

到目前为止,这是我想出的:

我的 jquery 代码:

<script type="text/javascript">
    $(function () {

        $("#getinfo").click(function () {

            $("input[name*=radio-choice-1]:checked").each(function () {
                 alert($(this).val());
            });

        });   

    });       

</script>

和单选按钮

<fieldset data-role="controlgroup" data-type="horizontal" data-role="fieldcontain">        
<input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" />
<label for="radio-choice-1">Yes</label>
<input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2" />
<label for="radio-choice-2">No</label>
</fieldset>

<fieldset data-role="controlgroup" data-type="horizontal" data-role="fieldcontain">        
<input type="radio" name="radio-choice-3" id="radio-choice-3" value="choice-3" />
<label for="radio-choice-3">Yes</label>
<input type="radio" name="radio-choice-3" id="radio-choice-4" value="choice-4" />
<label for="radio-choice-4">No</label>
</fieldset>

<fieldset data-role="controlgroup" data-type="horizontal" data-role="fieldcontain">        
<input type="radio" name="radio-choice-5" id="radio-choice-5" value="choice-5"  />
<label for="radio-choice-5">Yes</label>
<input type="radio" name="radio-choice-5" id="radio-choice-6" value="choice-6" />
<label for="radio-choice-6">No</label>
</fieldset>

<fieldset data-role="controlgroup" data-type="horizontal" data-role="fieldcontain">        
<input type="radio" name="radio-choice-7" id="radio-choice-7" value="choice-7" />
<label for="radio-choice-7">Yes</label>
<input type="radio" name="radio-choice-7" id="radio-choice-8" value="choice-8" />
<label for="radio-choice-8">No</label>
</fieldset>

<a href="#" id="getinfo">getinfo</a>

【问题讨论】:

  • 您的警报不起作用吗?还是您对如何将它们放入数组感到困惑?
  • 很抱歉我不够清楚。我的提醒不起作用

标签: jquery css jquery-mobile


【解决方案1】:

您正在查找名称包含文本 radio-choice-1 的选中单选按钮 - 这只会是一个单选按钮,因为它是全名。我认为您真正想要的是名称包含文本 radio-button- 的选中单选按钮,因此只需从选择器中删除 1 即可。

Working DEMO

【讨论】:

    【解决方案2】:

    这将完成工作。我在我的项目中使用下面的代码。正如 jQuery Mobile 文档中给出的那样简单直接。

    $("input[type='radio']").bind( "change", function(event, ui) {
            console.log($(this).val());
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多