<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();"我希望在我编写代码的按钮上获得所选的Radio的值
function getValue () {
alert("Value is :"+$('input[name=animal]:checked').val());
}但它不起作用,也不确定
更新:
我使用了Jquery的js和jquery的jquerymobile,这很好,但由于我将它用于移动应用程序,所以我需要两个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>解决了
发布于 2012-05-02 09:39:25
嗨,我想你是在不检查单选按钮的情况下得到这个值的。加载页面,然后选择任意一个单选按钮,然后调用getValue函数
或者,您可以在默认情况下放置任何选定的单选按钮。
例如:
将第一个单选按钮替换为
<input type="radio" name="animal" id="cat" value="Cat" checked="true"/>
<label for="cat">Cat</label>发布于 2012-05-02 10:07:06
试试这个$(“#=‘收音机’:checked”).val();
或
变量myRadio = $('inputname=myRadio');
//使用filter()函数,而不是find()。(find()用于定位子元素,而//filter()则搜索所选内容中的顶级元素。)。
变量checkedValue = myRadio.filter(':checked').val();
https://stackoverflow.com/questions/10410900
复制相似问题