我正在努力让 JQM 使用包含表单字段的 ListView 重新初始化 jquery 页面。
其中一个表单元素作为 JQM 正常初始化过程的一部分进行初始化,即正确显示并将所有正确的类应用于所有元素。
其他都是使用JQ的append动态添加的
动态添加元素时,我找不到正确的方法来重新初始化列表以将样式应用于所有内容,字段包含 div 标签和文本区域。
我准备了一个示例来展示重新初始化元素的不同方法,这些方法是我根据在 SO 和网络上发现的其他问题尝试过的。
http://jsfiddle.net/robaldred/UPsQr/
在我的示例中,第 5 行已正确初始化,但它需要调用 fieldcontain()
方法和 textinput()
方法,并手动添加 ui- input-label
类到标签。这感觉像是很多乱七八糟的事情,我一定错过了一些东西。
最佳答案
在页面上触发创建事件,如下所示:
$("#page").trigger("create");
Create vs. refresh: An important distinction
Note that there is an important difference between the create event and refresh method that some widgets have. The create event is suited for enhancing raw markup that contains one or more widgets. The refresh method should be used on existing (already enhanced) widgets that have been manipulated programmatically and need the UI be updated to match.
For example, if you had a page where you dynamically appended a new unordered list with data-role=listview attribute after page creation, triggering create on a parent element of that list would transform it into a listview styled widget. If more list items were then programmatically added, calling the listview’s refresh method would update just those new list items to the enhanced state and leave the existing list items untouched.
引用:http://jquerymobile.com/demos/1.0/docs/pages/page-scripting.html
更新了你的 fiddle - http://jsfiddle.net/UPsQr/2/
关于javascript - 如何告诉 jquery mobile 将类添加到 ListView 中动态添加的表单字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8837601/