【问题标题】:Checked Radio Button not updated in UI选中的单选按钮未在 UI 中更新
【发布时间】:2012-05-03 06:09:39
【问题描述】:

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

<!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 中更新。

【问题讨论】:

  • 我觉得和你之前的问题一样stackoverflow.com/q/10425246/705773
  • 是的,但它没有在 UI 中更新,问题是 span 。如何为绑定到特定单选按钮的特定 span 元素提供样式。如果我要添加类ui-radio-on 跨越它会更新所有 span 元素。关于这个有什么想法吗??

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


【解决方案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");

【讨论】:

  • 这是否适用于普通的 jquery。我的意思是不在 jquerymobile 中。我遇到了同样的问题,单选按钮将被选中,但它的选中的 html 属性没有更新。
猜你喜欢
  • 2021-06-14
  • 1970-01-01
  • 2019-06-08
  • 2014-09-01
  • 2021-08-05
  • 2012-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多