【问题标题】:Append Form And Elements jQuery Mobile追加表单和元素 jQuery Mobile
【发布时间】:2013-08-18 06:19:53
【问题描述】:

您好,我想附加一个表单和复选框元素,但该表单正在失去其样式。复选框应组合在“列表”中,保存出勤按钮应根据 jquery Mobile 设置样式。

请问我该如何解决这个问题?

看起来是这样的:

代码:

$('#editattendancecontent').append('<p>Who Attended?</p><form id="editattendanceform"><div data-role="fieldcontain"><fieldset data-role="controlgroup" id="editattendancelist"></fieldset></div><input type="submit" value="Save Attendance" data-inline="true" /></form>');

for (var i = 0; i < json.length; i++)
{
    $('#editattendancelist').append('<input type="checkbox" id=\"' + json[i].userID + '\" class="custom" /><label for=\"' + json[i].userID + '\">'+json[i].name+'</label>').trigger('create');

}
$('input: checkbox').checkboxradio("refresh");

【问题讨论】:

  • 截图没有出现!
  • 按钮$('#button_selector').button();和复选框$('[type=checkbox]').checkboxradio().trigger('create');

标签: jquery forms jquery-mobile mobile checkbox


【解决方案1】:

解决方案

两件事:

  1. 您需要删除trigger("create") 调用,每次循环遍历数据时都会这样做,这是完全错误的。该函数应该在动态内容的父级上调用。
  2. 由于您有多个需要增强的项目、按钮、复选框等,因此最好使用trigger("create"),而不是逐个增强组件。

如果您在加载页面时这样做,我希望您使用pageinit

代码

在所有这些变化之后,

标记:

<div data-role="page" id="mypage">
    <div data-role="header" data-theme="a">
         <h1></h1>
    </div>
    <div data-role="content" id="editattendancecontent"></div>
<div>

JS

//pageinit method of the page
$(document).on("pageinit", "#mypage", function () {
    var json = [
        {
        userID: Math.random(),
        name: "Roger"
        }, 
        {
        userID: Math.random(),
        name: "Summer"
        }, 
        {
        userID: Math.random(),
        name: "William"
        }, 
        {
        userID: Math.random(),
        name: "Sunny"
        }, 
        {
        userID: Math.random(),
        name: "Walter"
        }
    ];

    console.log(json);

    $('#editattendancecontent').append('<p>Who Attended?</p><form id="editattendanceform"><div data-role="fieldcontain"><fieldset data-role="controlgroup" id="editattendancelist"></fieldset></div><input type="submit" value="Save Attendance" data-inline="true" /></form>');

    for (var i = 0; i < json.length; i++) {
        $('#editattendancelist').append('<input type="checkbox" id="' + json[i].userID + '" class="custom" /><label for="' + json[i].userID + '">' + json[i].name + '</label>') //removed trigger("create");

    }
    $(this).trigger("create"); //where $(this) is the page - you can also call it on $("#editattendancecontent"). That'll also work
});

演示

http://jsfiddle.net/hungerpain/HMQf7/1/

【讨论】:

  • 然而,pageinit 只在页面加载时工作一次,如果我回到页面,它不会重新加载
  • 您需要将pageshow event.change pageinit 更改为pageshowpagebeforeshow,每次回来都会发生这种情况
  • 好的,非常感谢,如果可以的话我会给你+1000 哈哈
猜你喜欢
  • 2013-08-20
  • 2012-03-05
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 2013-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多