<input type="radio" name="animal" id="cat" value="Cat" />
<label for="cat">Cat</label>
<input type="radio" name="animal" id="radio-choice-2" value="choice-2"/>
<label for="radio-choice-2">Dog</label>
<input type="radio" name="animal" id="radio-choice-3" value="choice-3" />
<label for="radio-choice-3">Hamster</label>
<input type="radio" name="animal" id="radio-choice-4" value="choice-4" />
<label for="radio-choice-4">Lizard</label>
<input type="button" value="Get Radio Button Value" onclick="getValue();"
我希望在点击我编写代码的按钮
时获取所选单选按钮的值function getValue () {
alert("Value is :"+$('input[name=animal]:checked').val());
}
但它不起作用并获得未定义
更新:
我使用过Jquery和jquerymobile的js 对于jquery它的罚款,但因为我使用它的移动应用程序所以我需要两个js库所以在第二种情况下,它没有工作。
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script>
答案 0 :(得分:1)
嗨,我认为你没有检查任何单选按钮就获得了价值。 加载页面,然后选择任一单选按钮,然后调用getValue函数
或者,您可以默认选择任何单选按钮。
e.g。
用
替换第一个单选按钮<input type="radio" name="animal" id="cat" value="Cat" checked="true"/>
<label for="cat">Cat</label>
答案 1 :(得分:1)
试试这个 $(“#myform input [type ='radio']:checked”)。val();
OR
var myRadio = $('input [name = myRadio]');
//使用filter()函数,而不是find()。 (find()用于定位子元素,而// filter()用于搜索选择中的顶级元素。)
var checkedValue = myRadio.filter(':checked')。val();