简体   繁体   English

如何告诉jquery mobile将类添加到listview中动态添加的表单字段?

[英]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.

相关问题 如何将点击事件添加到JQuery中动态添加的元素? - How do I add click events to dynamically added elements in JQuery? 如何使用jquery验证动态填充的表单字段? - How do I use jquery to validate form fields, that was populated dynamically? 如何验证动态添加的字段并防止使用jQuery提交表单? - How to validate dynamically added fields and prevent form submission using jQuery? 动态将jquery移动组件添加到具有格式的listview - Dynamically add jquery mobile components to listview with formatting 动态添加内容到ListView jQuery Mobile - Dynamically add content to a listview jquery mobile 当添加不同的值时,如何使用jQuery来添加分段的表单字段并进行更新? - How can i use jQuery to add up sectioned form fields and update when different values are added? 动态添加ListView时jQuery Mobile页脚未修复 - jQuery Mobile footer not fixed when listview dynamically added 如何在Rails中将红宝石动态地添加到表单(一键单击2个)到表单中 - How do I dynamically add form fields (2 in one click) to my form in ruby on rails Jquery Mobile:当listview()函数不起作用时,如何为动态添加的列表项设置样式? - Jquery Mobile: How to style dynamically added list items when listview() function does not work? jQuery DataTables:如何为每个动态添加的行添加行ID? - jQuery DataTables: How do I add a row ID to each dynamically added row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM