在javascript中设置单选按钮值

ds3*_*345 1 html javascript jquery jquery-mobile

我试图通过javascript设置单选按钮的值.但我无法这样做.我试图做的是有4个单选按钮,其中一个已被选中.如果我选择其他单选按钮并单击"刷新",则应选择默认单选按钮.

http://jsfiddle.net/ds345/Un8XK/1/

HTML:

<fieldset data-role="controlgroup" data-type="vertical">
    <input type="radio" name="radio" id="x" data-theme="a" />
    <label for="x" style="color: White">X</label>
    <input type="radio" name="radio" id="y" onclick="axisonoff(this)" data-theme="a" />
    <label for="y" style="color: White">Y</label>
    <input type="radio" name="radio" id="z" data-theme="a" />
    <label for="z" >Z</label>
    <input type="radio" name="radio" id="none" data-theme="a" />
    <label for="none" style="color: White">None</label>
</fieldset>


<button id = "Refresh" value="Refresh">Refresh</button>
Run Code Online (Sandbox Code Playgroud)

JS:

$(document).ready(function () {
    $("#none").attr("checked", true).checkboxradio("refresh"); // if this line is not present initially then it works for the 1st refresh. 

});

$("#Refresh").click(function(){
        $("#x").attr("checked", false).checkboxradio("refresh");
        $("#y").attr("checked", false).checkboxradio("refresh");
        $("#z").attr("checked", false).checkboxradio("refresh");
    $("#none").attr("checked", true).checkboxradio("refresh");
});
Run Code Online (Sandbox Code Playgroud)

我确信我错过了一些非常小的但却无法弄清楚为什么这种方法不起作用.

使用的工具:Javascript,Jquery 1.9和JQuery mobile 1.3

谢谢,

Deeksha

Jam*_*lly 5

您应该使用propattr与布尔属性的时候.

.attr("checked", false)将添加checked="false"到您的元素.
在HTML中,与元素<input checked="false" .../>相同<input checked="true" .../>或简单,<input checked .../>因为元素只需要存在于元素上以使其处于活动状态.
请参阅此JSFiddle示例.

更改您要使用的代码.prop():

$("#none").prop("checked", false)...
Run Code Online (Sandbox Code Playgroud)

这是您的JSFiddle演示的固定版本:http://jsfiddle.net/Un8XK/8/