如何在jQuery mobile中获取已检查的单选按钮值?

时间:2012-02-24 15:59:23

标签: jquery css3 jquery-mobile

我有一组单选按钮组。如何为每个组检查是或现在检查哪个并将它们添加到阵列?

这是我到目前为止所得到的:

我的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>

2 个答案:

答案 0 :(得分:6)

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

Working DEMO

答案 1 :(得分:3)

这将完成工作。我在我的项目中使用以下代码。简单直接,如jQuery Mobile文档中所述。

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