如何在 jQuery Mobile 中获取选中的单选按钮值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9433871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get checked radio buttons value in jQuery mobile?
提问by Steven
I have a set of radio button groups. How can I get which is checked yes or now for each group and add them to array?
我有一组单选按钮组。我怎样才能得到每个组检查是或现在哪个并将它们添加到数组中?
Here is what I came up so far:
这是我到目前为止的想法:
My jquery code:
我的jQuery代码:
<script type="text/javascript">
$(function () {
$("#getinfo").click(function () {
$("input[name*=radio-choice-1]:checked").each(function () {
alert($(this).val());
});
});
});
</script>
and radio buttons
和单选按钮
<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>
回答by Anthony Grist
You're looking for the checked radio button with a name containing the text radio-choice-1- that's only ever going to be one radio button, because it's the full name. I think what you actually wanted is the checked radio buttons with a name containing the text radio-button-, so just remove the 1from your selector and it should work.
您正在寻找名称中包含文本的选中单选按钮radio-choice-1——那只会是一个单选按钮,因为它是全名。我认为您真正想要的是选中的单选按钮,其名称包含 text radio-button-,因此只需1从您的选择器中删除,它应该可以工作。
回答by Jay Mayu
This will do the work. I'm using the below code in my project. Simple and straight forward as given in the jQuery Mobile documentation.
这将完成工作。我在我的项目中使用以下代码。jQuery Mobile 文档中给出的简单直接。
$("input[type='radio']").bind( "change", function(event, ui) {
console.log($(this).val());
});

