仅使用 jquery 将自定义数据保存到 dom 元素一次

发布于 2024-12-09 03:48:54 字数 1882 浏览 2 评论 0原文

我通过读取查询字符串参数使用 jQuery 创建动态控件。可以拖动这些控件以整齐地放置它们,在拖/放中的放置事件之后,我将通过隐藏字段更新控件的位置和状态。只是我的实现现在每次都会更新数据,这不太好。我也通过事件做到了这一点,但效果不佳

当前实施

/*Create controls when only query string exists*/
if (config.params) {
    //parse the query string json as a object
    //ex:{type:"textbox",x:100,y:200,label:Name}
    var params = $.parseJSON(config.params);

    //for each control in json add it to document body
    $(params).each(function (idx, obj) {
        var id = new Date();//for generating dynamic id
        id = id.getTime();
        $("<span>").addClass(obj.css).load(function (ev) {
            var a = this;
            $(a).data("keys", {
                key: $("label", a).text()**certainly label wont be found**,
                value: $("input[name]:hidden", a)
            });
        }).
        easydrag().ondrop(function (e, el) {
            //easydrag drag drop plugin
            var a = $(el),
                b = a.data("keys"),
                key = b.key,
                value = b.value;
            value.val(key + "$" + e.pageX + "$" + e.pageY);
        }).
        css({
            position: "absolute",
            top: obj.y,
            left: obj.x
        }).
        append($("<label>", {
            "for": id
        }).
         html(obj.label).
         append(config.ELEMENTS(obj.type).attr("id", id))).
         append($("<input>", {
            "type": "hidden",
            "name": "CONTROL$POSITION",
            "id": "control_position_" + idx
         })).
        appendTo("form");
    });
} 

问题

  1. 正如您所看到的,我在加载时将数据附加到 span 元素。但由于它是加载事件,我不会有内部标签和隐藏字段。我只需要在 elementspan 上设置数据一次,因为在 drop 事件 期间我将访问标签和隐藏字段以更新控件的当前位置。我不想想要查询每个drop上的dom以获取标签隐藏字段,这就是我的目的。
  2. 还请给我一些关于如何在回发后重绘控件的想法?

I am creating dynamic controls using jQuery by reading the query string parameter. These controls can be dragged to lay them in neat manner, after the drop event in drag/drop I would update the position and state of control through hidden field. It's just that my implementation now updates data at each drop which is not nice. I also did this through event but did not work well

Current Implementation

/*Create controls when only query string exists*/
if (config.params) {
    //parse the query string json as a object
    //ex:{type:"textbox",x:100,y:200,label:Name}
    var params = $.parseJSON(config.params);

    //for each control in json add it to document body
    $(params).each(function (idx, obj) {
        var id = new Date();//for generating dynamic id
        id = id.getTime();
        $("<span>").addClass(obj.css).load(function (ev) {
            var a = this;
            $(a).data("keys", {
                key: $("label", a).text()**certainly label wont be found**,
                value: $("input[name]:hidden", a)
            });
        }).
        easydrag().ondrop(function (e, el) {
            //easydrag drag drop plugin
            var a = $(el),
                b = a.data("keys"),
                key = b.key,
                value = b.value;
            value.val(key + "$" + e.pageX + "$" + e.pageY);
        }).
        css({
            position: "absolute",
            top: obj.y,
            left: obj.x
        }).
        append($("<label>", {
            "for": id
        }).
         html(obj.label).
         append(config.ELEMENTS(obj.type).attr("id", id))).
         append($("<input>", {
            "type": "hidden",
            "name": "CONTROL$POSITION",
            "id": "control_position_" + idx
         })).
        appendTo("form");
    });
} 

Question

  1. As you see I attach data to span element on load. But since it is load event i won't have the inner label and hidden fields. I need to set the data on elementspan only once, since during drop event i will access the label and hidden field to update the current position of the control. I don't want to query the dom on each drop for the label and hidden field that's my purpose.
  2. Also give me ideas on how I can redraw the controls after a postback?

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

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

发布评论

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

评论(1

暗恋未遂 2024-12-16 03:48:54

摆脱load,并临时存储对该元素的引用。元素完全创建完成后执行代码:

var span = $("<span>");
span.addClass(obj.css).
    easydrag(). //et cetera
    ....
    appendTo("form");

//After the element has finished:
$(span).data("keys", {
    key: $("label", span).text(),
    value: $("input[name]:hidden", span)
});

Get rid of load, and temporary store a reference to the element. Execute the code after the element has fully finished creating:

var span = $("<span>");
span.addClass(obj.css).
    easydrag(). //et cetera
    ....
    appendTo("form");

//After the element has finished:
$(span).data("keys", {
    key: $("label", span).text(),
    value: $("input[name]:hidden", span)
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文