追加/删除/替换
我不确定这是否是编写此代码的最佳方式,但我的几乎有效...
这就是我想要做的:
(1) 移动 元素在单击时移动到另一个 div
(2) 通过附加或删除
来更新隐藏的输入字段注意:我不知道不想替换隐藏输入字段中的值,而是添加到其中,这样您就可以得到“u40,u56,u98”的值......等等......
所以,当用户单击具有 addable 类的
元素,它会从其父
元素的 id 进行更新。相反,如果用户单击具有可移动类的
- 元素,则会发生相反的情况...
- 将从其父元素中删除 < code>
- 并附加到另一个,隐藏的输入字段通过删除
元素的 id 进行更新...
中删除,并附加到
中另一个分区。此外,隐藏的输入字段将使用
这就是我所拥有的远...添加它时它可以工作,但是当您单击 li.removable 元素时,隐藏的输入字段不会删除该值。
$('li').click(function() {
var uID = $(this).attr('id')+',';
if($(this).hasClass("addable")) {
$("input#edit-r").val($("input#edit-r").val() + uID);
$(this).removeClass("addable");
$(this).addClass("removable");
$(this).appendTo($("#selections ul"));
} else {
var currentValue = $("input#edit-r").val();
currentValue.replace(uID,"");
$("input#edit-r").val(currentValue);
$(this).removeClass("removable");
$(this).addClass("addable");
$(this).appendTo($(".filtered ul"));
}
});
<div class="filtered">
<ul>
<li id="u40" class="addable">U40</li>
<li id="u56" class="addable">U56</li>
<li id="u98" class="addable">U98</li>
</ul>
</div>
<div id="selections">
<ul>
</ul>
</div>
<input type="hidden" id="edit-r" value="" />
I'm not sure if this is the most optimal way of writing this code, but what I have almost works...
This is what I'm trying to do:
(1) Move the <li></li>
element to another div when clicked
(2) Update a hidden input field by either appending or removing the <li id>
Note: I don't want to replace the value in the hidden input field, but add to it, so that you can wind up with a value of "u40,u56,u98" ... etc...
So, when a user clicks on an <li>
element with the class of addable, it's removed from it's parent <ul>
and appended to the <ul>
in another div. Additionally, the hidden input field will update with the id of the <li>
element. Conversely, if a user clicks on an <li>
element with a class of removable, the opposite happens... The <li>
is removed from it's parent <ul>
and appended to the other one, and the hidden input field updates by removing the id of the <li>
element...
This is what I have so far... It works when you add it, but the hidden input field doesn't remove the value when you click on the li.removable element.
$('li').click(function() {
var uID = $(this).attr('id')+',';
if($(this).hasClass("addable")) {
$("input#edit-r").val($("input#edit-r").val() + uID);
$(this).removeClass("addable");
$(this).addClass("removable");
$(this).appendTo($("#selections ul"));
} else {
var currentValue = $("input#edit-r").val();
currentValue.replace(uID,"");
$("input#edit-r").val(currentValue);
$(this).removeClass("removable");
$(this).addClass("addable");
$(this).appendTo($(".filtered ul"));
}
});
<div class="filtered">
<ul>
<li id="u40" class="addable">U40</li>
<li id="u56" class="addable">U56</li>
<li id="u98" class="addable">U98</li>
</ul>
</div>
<div id="selections">
<ul>
</ul>
</div>
<input type="hidden" id="edit-r" value="" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改
为
另外为了优化您的代码,您可以使用 jquery 对象可链接性。
change
To
Also for optimizing your code you may use jquery objects chainability.
与其在每次做出选择时尝试更新隐藏输入,为什么不让 onsubmit 处理程序为您处理这项工作呢?
并且:
通过这种方式,您不必担心隐藏元素与所选项目保持同步。
Instead of trying to update the hidden input every time a selection is made, why not have an onsubmit handler take care of this work for you?
And:
In this manner, you don't have to worry about your hidden element staying in sync with the selected items.