admin管理员组文章数量:1390634
How can I temporarily store an array of string values most elegantly in a form?
I have a form, where a user can edit an article - and add tags, which are simply string values.
I don't want to persist it until the user actually saves the entire article, so I need to be able to temporarily ...:
- Display the list of selected tags
- Add a tag to the list
- Remove a tag from the list
- Submit the list of values when I save the form
I could store everything in just a ma-separated hidden field, but it seems ugly, and I would prefer something stronger typed.
What is the right way to do this? Pointers to examples very wele.
How can I temporarily store an array of string values most elegantly in a form?
I have a form, where a user can edit an article - and add tags, which are simply string values.
I don't want to persist it until the user actually saves the entire article, so I need to be able to temporarily ...:
- Display the list of selected tags
- Add a tag to the list
- Remove a tag from the list
- Submit the list of values when I save the form
I could store everything in just a ma-separated hidden field, but it seems ugly, and I would prefer something stronger typed.
What is the right way to do this? Pointers to examples very wele.
Share Improve this question edited Sep 30, 2009 at 11:21 Alex Barrett 16.5k4 gold badges54 silver badges52 bronze badges asked Sep 30, 2009 at 11:09 KjensenKjensen 12.4k39 gold badges114 silver badges179 bronze badges1 Answer
Reset to default 9JQuery provides the data
method for precisely these situations! It works directly with native JavaScript objects, so no need to mess about with ma separated lists - just use an array or object. Perhaps something like this will get you on the right track.
// initialize existing tags
$('#form').data('tags', { foo: true, bar: true });
// add a new tag
$('#form').data('tags').baz = true;
// remove an existing tag
$('#form').data('tags').bar = false;
$('#form').data('tags');
// { foo: true, bar: false, baz: true }
Having removed tags remain in the object as false
will help you see which tags need unassigning server-side; not necessary but potentially useful to you.
If you would rather the removed values were removed pletely from the object, use the delete
construct.
// remove an existing tag
delete $('#form').data('tags').bar;
Edit: In an attempt to address your ments on this answer and give you some ideas:
<ul class="tags">
<li>
<span class="tag-name">foo</span>
<a href="#" class="tag-remove">remove</a>
</li>
</ul>
And your JavaScript:
$(function() {
$('#form .tag-remove').click(function(e) {
// update the tag data
var tag = $(this).siblings('.tag-name').text();
$('#form').data('tags')[tag] = false;
// remove the tag element
$(this).parent().remove();
return false;
});
});
There will be more of course - this does not handle the initialization of the form's tag data, the addition of new tags, or the submitting of tags to the server - but hopefully it will nudge you in the right direction :)
本文标签: javascriptEasiest way to temporarily store keyvalue pairs in a form using JQueryStack Overflow
版权声明:本文标题:javascript - Easiest way to temporarily store key-value pairs in a form using JQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744617831a2615840.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论