首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选中的单选按钮未在UI中更新

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

Stack Overflow用户
提问于 2012-05-03 14:09:39
回答 1查看 2.3K关注 0票数 0

我已经用Jquery选中了单选按钮,但它在UI中没有更新。任何人都知道.below是我的代码片段。

代码语言:javascript
复制
<!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中没有更新。

EN

回答 1

Stack Overflow用户

发布于 2012-05-03 15:05:11

正如Chase在回答您的另一个问题时所说的,当您更改底层jQuery元素的checked属性时,必须刷新form Mobile小部件。这可以通过编写以下代码实现:

代码语言:javascript
复制
$("input[name=animal][id=radio-choice-2]")
    .prop("checked", true).checkboxradio("refresh");

这样,您就不必自己操作类了,所有的工作都由移动小部件完成。

注意,在传递过程中,您的选择器针对的是一个具有id属性的元素,因此可以简化它:

代码语言:javascript
复制
$("#radio-choice-2").prop("checked", true).checkboxradio("refresh");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10426019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档