【问题标题】:Jquery Mobile checkbox-list does not update when adding items and "refreshing" the script to style it添加项目并“刷新”脚本以设置样式时,Jquery Mobile 复选框列表不会更新
【发布时间】:2017-05-15 05:10:48
【问题描述】:

在 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/

请帮帮我,谢谢!

【问题讨论】:

    标签: javascript jquery html jquery-mobile mobile


    【解决方案1】:

    不要惊慌。当您使用 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

    【讨论】:

      猜你喜欢
      • 2013-05-07
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      相关资源
      最近更新 更多