选中的单选按钮未在UI中更新

时间:2012-05-03 06:09:39

标签: javascript jquery html5 jquery-mobile radio-button

我已经使用Jquery检查了单选按钮但是它没有在UI中更新。 任何人都有任何想法。下面是我的片段......

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Radio Button</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.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/jquery.mobile-1.1.0.min.js"></script>
    <script>
    function getValue() {  
    alert("Value is :"+$('input[name=animal]:checked').val());
    }

    function setValue(toBeSetID) {  
    $("input[name=animal][id=radio-choice-2]").prop("checked", true);
    $("label[for='rock']").addClass("ui-radio-on");
    alert("Value is :"+$('input[name=animal]:checked').val());
    }
    </script>
    </head> 
<body> 
<div data-role="page">
<div data-role="header">
    <h1>Radio Button</h1>
</div><!-- /header -->
<div data-role="content">   
    <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();">
    <input type="button" value="Set Radio Button Value" onclick="setValue();">
</div><!-- /content -->
</div><!-- /page -->
</body>
</html>

由程序检查为警告框但未在UI中更新。

1 个答案:

答案 0 :(得分:1)

正如Chase在回答您的其他问题时所说,当您更改基础表单元素的checked属性时,您必须刷新jQuery Mobile小部件。这可以通过写:

来完成
$("input[name=animal][id=radio-choice-2]")
    .prop("checked", true).checkboxradio("refresh");

这样,您不必自己操纵类,移动小部件可以完成所有工作。

注意,传递选择器的目标是具有id属性的元素,因此可以简化:

$("#radio-choice-2").prop("checked", true).checkboxradio("refresh");
相关问题