从pageinit检查单选按钮不起作用

时间:2012-08-11 03:45:49

标签: jquery-mobile radio-button page-init

以下代码不起作用,未选中单选按钮。知道为什么吗?

我也试过attr('checked', 'checked')

<html>
<head>      
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.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.1/jquery.mobile-1.1.1.min.js"></script>      
    <script type="text/javascript">
    $(document).bind("pageinit", function () {           
        $('#radio-pet-2a').attr('checked', true);
    });         
    </script>
  </head>
 <body>

<div data-role="content">

    <ul data-role = "listview">

        <li>
        <fieldset data-role="controlgroup">
        <legend>Choose a pet:</legend>
             <input type="radio" name="radio-pet" id="radio-pet-1a" value="choice-1"   />
             <label for="radio-pet-1a">Cat</label>

             <input type="radio" name="radio-pet" id="radio-pet-2a" value="choice-2"  />
             <label for="radio-pet-2a">Dog</label>

             <input type="radio" name="radio-pet" id="radio-pet-3a" value="choice-3"  />
             <label for="radio-pet-3a">Hamster</label>

             <input type="radio" name="radio-pet" id="radio-pet-4a" value="choice-4"  />
             <label for="radio-pet-4a">Lizard</label>
    </fieldset>
        </li>

    </ul>
</div>

</body>
</html>

3 个答案:

答案 0 :(得分:2)

尝试 prop 方法设置单选按钮的属性检查,然后刷新单选按钮。

$(document).bind("pageinit", function () {           
    $('#radio-pet-2a').prop("checked", true).checkboxradio("refresh");
});

答案 1 :(得分:0)

更改逻辑设置后,您需要使用button('refresh')更新渲染:

$(document).bind("pageinit", function () {           
    $('#radio-pet-2a').attr('checked', true).button('refresh');
});

答案 2 :(得分:0)

我已经想出如何让它发挥作用:

    $(document).bind("pageinit", function () {           
        $('#radio-pet-2a').attr('checked', true);
        $("input[type='radio']").checkboxradio("refresh"); 
    });