带有动态字段的 jQueryMobile 和 datebox 插件

发布于 2024-12-22 06:00:10 字数 4103 浏览 1 评论 0原文

使用 JQueryMobile 最终版本和 jquery.mobile.datebox.js 插件。在 pageshow 事件中,我动态添加一系列字段,其中一些是使用 datebox 插件的日期/时间字段。一切工作正常,除了在较小的设备上,尤其是 iPhone 上(当您将桌面浏览器设置得特别窄时,也会发生这种情况)。当您单击日期或时间图标时,会弹出帮助程序,我可以选择一个日期/时间,它会进入该字段,但该字段会与 .

这是页面显示代码的示例。它调用 Web 服务来获取 json 数据,该数据定义了应添加到表单中的字段。这些字段可以是日期、单选或文本。除了弹出窗口后重新绘制日期/时间字段之外,一切正常:

$('#editthing').live('pageshow', function (e, info) {
    var listurl = "/editthingattribute";
    //Add a cache 'buster' parameter
    var date = new Date();
    listurl = listurl + '?p_uid=' + date.getTime();
    $.ajax({
        url: listurl + "&p_id=" + getUrlVar("p_id") + "&p_thingattribute=" + getUrlVar("p_thingattribute"),
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        success: function (data) {
            var form = $("#formcontent");
            form.html = "";
            //Add the fields defined in the results from the database call
            $.each(data.editthingfields, function (index, item) {
                //Set the global field type
                fieldType = item.fieldType;

                if (item.fieldtype == 'date') {
                    inputType = 'date';

                    //This messes up on narrow browsers
                    form.append('<div data-role="fieldcontain">' + '<label for="' + item.fieldname + '_date">' + item.fieldlabel + ' (Date)</label>' + '<input name="' + item.fieldname + '_date" id="' + item.fieldname + '_date" ' + 'type="date" data-role="datebox" data-options=\'{"mode": "calbox", "dateFormat":"dd/mm/YYYY"}\' ' + 'value="' + item.fieldvalue.substring(0, 10) + '" />' + '</div>' + '<div data-role="fieldcontain">' + '<label for="' + item.fieldname + '_time">' + item.fieldlabel + ' (Time)</label>' + '<input name="' + item.fieldname + '_time" id="' + item.fieldname + '_time" ' + 'type="date" data-role="datebox" data-options=\'{"mode": "timebox"}\' ' + 'value="' + item.fieldvalue.substring(11, 17) + '" />' + '</div>');
                } else if (item.fieldtype == 'radio') {
                    inputType = 'radio';
                    //Add a suitable set of radio buttons for the attribute being edited.
                    form.append('<div data-role="fieldcontain">' + '<fieldset data-role="controlgroup">' + '<legend>' + item.fieldlabel + '</legend>');
                    var optionsarray = item.fieldoptions.split(',');
                    for (var i = 0; i < optionsarray.length; i++) {
                        if (item.fieldvalue == optionsarray[i]) {
                            form.append('<input name="' + item.fieldname + '" id="' + optionsarray[i] + '" ' + 'type="' + item.fieldtype + '" value="' + optionsarray[i] + '" checked="checked" />');
                        } else {
                            form.append('<input name="' + item.fieldname + '" id="' + optionsarray[i] + '" ' + 'type="' + item.fieldtype + '" value="' + optionsarray[i] + '" />');
                        }
                        form.append('<label for="' + optionsarray[i] + '">' + optionsarray[i] + '</label>');
                    }
                    form.append('</fieldset>' + '</div>');
                } else {
                    inputType = 'text';
                    form.append('<div data-role="fieldcontain">' + '<label for="' + item.fieldname + '">' + item.fieldlabel + '</label>' + '<input name="' + item.fieldname + '" id="' + item.fieldname + '" ' + 'type="' + item.fieldtype + '" value="' + item.fieldvalue + '" />' + '</div>');
                }
            });
            var thinghdr = $("#thingheader");
            thinghdr.html = "";
            $(data.thingdetails).each(function (index) {
                thinghdr.append("<h3>" + this.thingoper + " " + this.thingnumber + " " + this.thingdesc + "</h3>");
            });

            $("#editthing").page("destroy").page();
        }
    });
});

我猜问题是由于动态添加字段造成的,但我不知道如何解决它。

提前致谢 麦克风

Using JQueryMobile final release and the jquery.mobile.datebox.js plugin. On the pageshow event I am adding a series of fields dynamically, some of these are date/time fields using the datebox plugin. Everything works fine except on smaller devices and iPhone in particular (it also happens with desktop browsers when you make them particularly narrow). When you click on the date or time icon the helper pops up, I can select a date/time and it goes into the field but the field then gets duplicated along with anything else in the .

Here's an example of the page show code. It calls a webservice to get json data back that defined the fields that should be added to the form. The fields can be date, radio or text. It all works fine except the redrawing of the date/time fields after the popup:

$('#editthing').live('pageshow', function (e, info) {
    var listurl = "/editthingattribute";
    //Add a cache 'buster' parameter
    var date = new Date();
    listurl = listurl + '?p_uid=' + date.getTime();
    $.ajax({
        url: listurl + "&p_id=" + getUrlVar("p_id") + "&p_thingattribute=" + getUrlVar("p_thingattribute"),
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        success: function (data) {
            var form = $("#formcontent");
            form.html = "";
            //Add the fields defined in the results from the database call
            $.each(data.editthingfields, function (index, item) {
                //Set the global field type
                fieldType = item.fieldType;

                if (item.fieldtype == 'date') {
                    inputType = 'date';

                    //This messes up on narrow browsers
                    form.append('<div data-role="fieldcontain">' + '<label for="' + item.fieldname + '_date">' + item.fieldlabel + ' (Date)</label>' + '<input name="' + item.fieldname + '_date" id="' + item.fieldname + '_date" ' + 'type="date" data-role="datebox" data-options=\'{"mode": "calbox", "dateFormat":"dd/mm/YYYY"}\' ' + 'value="' + item.fieldvalue.substring(0, 10) + '" />' + '</div>' + '<div data-role="fieldcontain">' + '<label for="' + item.fieldname + '_time">' + item.fieldlabel + ' (Time)</label>' + '<input name="' + item.fieldname + '_time" id="' + item.fieldname + '_time" ' + 'type="date" data-role="datebox" data-options=\'{"mode": "timebox"}\' ' + 'value="' + item.fieldvalue.substring(11, 17) + '" />' + '</div>');
                } else if (item.fieldtype == 'radio') {
                    inputType = 'radio';
                    //Add a suitable set of radio buttons for the attribute being edited.
                    form.append('<div data-role="fieldcontain">' + '<fieldset data-role="controlgroup">' + '<legend>' + item.fieldlabel + '</legend>');
                    var optionsarray = item.fieldoptions.split(',');
                    for (var i = 0; i < optionsarray.length; i++) {
                        if (item.fieldvalue == optionsarray[i]) {
                            form.append('<input name="' + item.fieldname + '" id="' + optionsarray[i] + '" ' + 'type="' + item.fieldtype + '" value="' + optionsarray[i] + '" checked="checked" />');
                        } else {
                            form.append('<input name="' + item.fieldname + '" id="' + optionsarray[i] + '" ' + 'type="' + item.fieldtype + '" value="' + optionsarray[i] + '" />');
                        }
                        form.append('<label for="' + optionsarray[i] + '">' + optionsarray[i] + '</label>');
                    }
                    form.append('</fieldset>' + '</div>');
                } else {
                    inputType = 'text';
                    form.append('<div data-role="fieldcontain">' + '<label for="' + item.fieldname + '">' + item.fieldlabel + '</label>' + '<input name="' + item.fieldname + '" id="' + item.fieldname + '" ' + 'type="' + item.fieldtype + '" value="' + item.fieldvalue + '" />' + '</div>');
                }
            });
            var thinghdr = $("#thingheader");
            thinghdr.html = "";
            $(data.thingdetails).each(function (index) {
                thinghdr.append("<h3>" + this.thingoper + " " + this.thingnumber + " " + this.thingdesc + "</h3>");
            });

            $("#editthing").page("destroy").page();
        }
    });
});

I'm guessing the problem is due to the fields being added dynamically but I don't know how to get round it.

thanks in advance
Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戈亓 2024-12-29 06:00:11

您可能已经超越了这一点,但我发现更新元素的 jQuery 移动方式是在它们上调用 create 。也许它会帮助您获得您感兴趣的行为,而不是 page("destroy").page();

在我将动态元素附加到存在中后,我调用触发器("create")像这样:

$(".selector").append(elementToAppend).trigger("create");

这是来自 jQuery mobile 文档页面 这里

增强新标记

页面插件调度 pageInit 事件,
大多数小部件使用它来自动初始化自己。只要一个
引用widget插件脚本,它会自动增强任何
它在页面上找到的小部件的实例。

但是,如果您在客户端生成新的标记或通过以下方式加载内容
Ajax并将其注入到页面中,可以触发create事件
处理其中包含的所有插件的自动初始化
新的标记。这可以在任何元素上触发(甚至是页面
div 本身),省去了手动初始化每个插件的任务
(列表视图按钮、选择等)。

例如,如果加载了 HTML 标记块(例如登录表单)
通过Ajax,触发create事件自动变换
它包含的所有小部件(在本例中为输入和按钮)到
增强版本。此场景的代码为:

$( ...包含小部件的新标记... ).appendTo( ".ui-page"
).trigger( "创建" );

You might have moved on past this, but I have found that the jQuery mobile way of updating elements is to call create on them. Perhaps it will help you have the behavior you're interested in intsead of page("destroy").page();

After I append my dynamic element into being, I call trigger("create") on it like this:

$(".selector").append(elementToAppend).trigger("create");

This is from the jQuery mobile documentation page here.

Enhancing new markup

The page plugin dispatches a pageInit event,
which most widgets use to auto-initialize themselves. As long as a
widget plugin script is referenced, it will automatically enhance any
instances of the widgets it finds on the page.

However, if you generate new markup client-side or load in content via
Ajax and inject it into a page, you can trigger the create event to
handle the auto-initialization for all the plugins contained within
the new markup. This can be triggered on any element (even the page
div itself), saving you the task of manually initializing each plugin
(listview button, select, etc.).

For example, if a block of HTML markup (say a login form) was loaded
in through Ajax, trigger the create event to automatically transform
all the widgets it contains (inputs and buttons in this case) into the
enhanced versions. The code for this scenario would be:

$( ...new markup that contains widgets... ).appendTo( ".ui-page"
).trigger( "create" );

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文