简体   繁体   中英

Saving custom data to dom elements using jquery only once

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 element span 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?

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)
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM