仅使用 jquery 将自定义数据保存到 dom 元素一次
我通过读取查询字符串参数使用 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");
});
}
问题
- 正如您所看到的,我在加载时将数据附加到 span 元素。但由于它是加载事件,我不会有内部标签和隐藏字段。我只需要在 element
span
上设置数据一次,因为在drop 事件
期间我将访问标签和隐藏字段以更新控件的当前位置。我不想想要查询每个drop上的dom
以获取标签
和隐藏字段
,这就是我的目的。 - 还请给我一些关于如何在回发后重绘控件的想法?
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
- As you see I attach data to span element on load. But since it is load event i won't have the inner
label
andhidden fields
. I need to set the data on elementspan
only once, since duringdrop event
i will access the label and hidden field to update the current position of the control. I don't want toquery the dom
on each drop for thelabel
andhidden field
that's my purpose. - Also give me ideas on how I can redraw the controls after a postback?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
摆脱
load
,并临时存储对该元素的引用。元素完全创建完成后执行代码:Get rid of
load
, and temporary store a reference to the element. Execute the code after the element has fully finished creating: