添加项目时,Jquery Mobile 复选框列表不会更新,并"refreshing"脚本来设置其样式



JQuery Mobile中添加复选框列表非常容易,脚本会自动设置/更改以下html的样式并赋予其功能:

<fieldset data-role="controlgroup" id="liste" class="liste">
        <legend>Choose as many snacks as you'd like:</legend>
        <input type="checkbox" name="checkbox-1a" id="checkbox-1a" class="custom">
        <label for="checkbox-1a">Cheetos</label>
        <input type="checkbox" name="checkbox-2a" id="checkbox-2a" class="custom">
        <label for="checkbox-2a">Doritos</label>
        <input type="checkbox" name="checkbox-3a" id="checkbox-3a" class="custom">
        <label for="checkbox-3a">Fritos</label>
        <input type="checkbox" name="checkbox-4a" id="checkbox-4a" class="custom">
        <label for="checkbox-4a">Sun Chips</label>
</fieldset>

但是,更改现有列表证明很困难,因为必须调用这种不想为我工作的刷新方法:

$(".selector").checkboxradio( "refresh" );  

这是我在 JsFiddle 中尝试将项目添加到列表中

失败的列表:

http://jsfiddle.net/b92anmqw/

请帮助我,谢谢!

不要惊慌。当您使用jQuery Mobile时,这也可以很容易地完成。首先,您应该设置页面的结构:

<div data-role="page" id="page-1">
  <div data-role="main" class="ui-content">
      ...here goes your fieldset
  </div>
</div>

您应该将新复选框附加到控制组容器内:

$(document).on("pagecreate", "#page-1", function() {
    var item5 = $("<label for='checkbox-5a'>Flips</label><input type='checkbox' id='checkbox-5a'></input>"), 
        liste = $("#liste");
    liste.controlgroup("container").append(item5);
    $(item5[1]).checkboxradio();
    liste.controlgroup("refresh");
});

顺便说一句,您也可以使用 $(document).on("pagecreate") 而不是 $(document).ready .

最新更新