[英]How do I tell jquery mobile to add classes to dynamically added form fields in a listview?
I am struggling to get JQM to reinitialize a jquery page with a listview that contains form fields. 我正在努力让JQM重新初始化一个包含表单字段的listview的jquery页面。
One of the form elements is initialized as part of JQM's normal initialization process, that is displaying correctly with all the correct class applied to all elements. 其中一个表单元素作为JQM正常初始化过程的一部分进行初始化,即正确显示应用于所有元素的所有正确类。
The others are all added dynamically using JQ's append 其他都是使用JQ附加动态添加的
When elements are added dynamically I cannot find the correct method to reinitialize the list to apply the styling to everything, the fieldcontain div the label & the textarea. 当动态添加元素时,我找不到重新初始化列表以将样式应用于所有内容的正确方法,fieldcontain div标签和textarea。
I have prepared an example to show the different methods to reinitialize the elements that I've tried based on other questions found on SO and around the web. 我已经准备了一个示例来展示根据在SO和网络上发现的其他问题重新初始化我尝试的元素的不同方法。
http://jsfiddle.net/robaldred/UPsQr/ http://jsfiddle.net/robaldred/UPsQr/
In my example, row 5 is correctly initialize, however it requires calling the fieldcontain()
method the textinput()
method and manually add the ui-input-label
class to the label. 在我的示例中,第5行正确初始化,但是它需要调用
fieldcontain()
方法textinput()
方法并手动将ui-input-label
类添加到标签。 This feels like a lot of messing about, I must be missing something. 这感觉好像很乱,我一定是在遗漏一些东西。
Trigger a create event on the page like this: 在页面上触发创建事件,如下所示:
$("#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.
请注意,某些小部件具有的create event和refresh方法之间存在重要差异。 The create event is suited for enhancing raw markup that contains one or more widgets.
create事件适用于增强包含一个或多个小部件的原始标记。 The refresh method should be used on existing (already enhanced) widgets that have been manipulated programmatically and need the UI be updated to match.
刷新方法应该用于已经以编程方式操作的现有(已经增强的)小部件,并且需要更新UI以匹配。
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.
例如,如果您有一个页面,您在页面创建后动态添加了一个带有data-role = listview属性的新的无序列表,则在该列表的父元素上触发create会将其转换为listview样式的小部件。 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.
如果以编程方式添加更多列表项,则调用listview的refresh方法会将这些新列表项更新为增强状态,并保持现有列表项不变。
Referenece: http://jquerymobile.com/demos/1.0/docs/pages/page-scripting.html 参考: http : //jquerymobile.com/demos/1.0/docs/pages/page-scripting.html
Updated your fiddle - http://jsfiddle.net/UPsQr/2/ 更新了你的小提琴 - http://jsfiddle.net/UPsQr/2/
Working Example: 工作实例:
JS JS
$('#add_element').click(function() {
var list = $('ul[data-role="listview"]');
var nextLi = ((list.children().length) + 1);
li = '<li><div data-role="fieldcontain"><label for="textarea'+nextLi+'">Input:</label><textarea id="textarea'+nextLi+'" name="textarea'+nextLi+'"></textarea></div></li>';
list.append(li);
list.listview('refresh');
$('#page').trigger('create');
});
//$('#add_element').hide();
HTML HTML
<html>
<head>
</head>
<body>
<div id="page" data-role="page">
<!-- First field is done by jQM's normal initialization -->
<!-- The Rest are added onload and appended to the listview -->
<ul data-role="listview">
<li>
<div data-role="fieldcontain">
<label for="textarea1">Input:</label>
<textarea id="textarea1" name="textarea1"></textarea>
</div>
</li>
</ul>
<a data-role="button" id="add_element">Add Fields</a>
</div>
</body>
</html>
NOTE: 注意:
jQM Supports jQuery 1.6.4 jQM支持jQuery 1.6.4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.