jquery 删除选定的元素并附加到另一个元素
我正在尝试将“已删除的选项”重新附加到适当的选择选项菜单。
我有三个选择框:“类别”、“变量”和“目标”。 “类别”是一个链接选择,因此当用户从中选择一个选项时,“变量”选择框将填充特定于所选类别选项的选项。当用户从“变量”选择框中选择一个选项时,它将附加到“目标”选择框。我有一个“删除选定的”功能,这样如果用户从“目标”选择框中“删除”选定的元素,它就会从“目标”中删除并放回到“变量”选项池中。我遇到的问题是它不加区别地将选项附加到“变量”项。也就是说,如果所选类别是“年龄”,则“变量”选项都具有“年龄”类别。但是,如果删除的选项是“收入”项目,它将显示在“年龄变量”选项列表中。
这是 HTML 标记:
<select multiple="" id="categories" name="categories[]">
<option class="category" value="income">income</option>
<option class="category" value="gender">gender</option>
<option class="category" value="age">age</option>
</select>
<select multiple="multiple" id="variables" name="variables[]">
<option class="income" value="10">$90,000 - $99,999</option>
<option class="income" value="11">$100,000 - $124,999</option>
<option class="income" value="12">$125,000 - $149,999</option>
<option class="income" value="13">Greater than $149,999</option>
<option class="gender" value="14">Male</option>
<option class="gender" value="15">Female</option>
<option class="gender" value="16">Ungendered</option>
<option class="age" value="17">Ages 18-24</option>
<option class="age" value="18">Ages 25-34</option>
<option class="age" value="19">Ages 35-44</option>
</select>
<select height="60" multiple="multiple" id="target" name="target[]">
</select>
并且,这是 js:
/* This determines what options are display in the "Variables" select box */
var cat = $('#categories');
var el = $('#variables');
$('#categories option').click(function() {
var class = $(this).val();
$('#variables option').each(function() {
if($(this).hasClass(class)) {
$(this).show();
} else {
$(this).hide();
}
});
});
/* This adds the option to the target select box if the user clicks "add" */
$('#add').click(function() {
return !$('#variables option:selected').appendTo('#target');
});
/* This is the remove function in its current form, but doesn't append correctly */
$('#remove').click(function() {
$('#target option:selected').each(function() {
var class = $(this).attr('class');
if($('#variables option').hasClass(class)) {
$(this).appendTo('#variables');
sortList('variables');
}
});
});
I'm trying to re-append a "removed option" to the appropriate select option menu.
I have three select boxes: "Categories", "Variables", and "Target". "Categories" is a chained select, so when the user selects an option from it, the "Variables" select box is populated with options specific to the selected categories option. When the user chooses an option from the "Variables" select box, it's appended to the "Target" select box. I have a "remove selected" feature so that if a user "removes" a selected element from the "Target" select box, it's removed from "Target" and put back into the pool of "Variables" options. The problem I'm having is that it appends the option to the "Variables" items indiscriminately. That is, if the selected category is "Age" the "Variables" options all have a class of "age". But, if the removed option is an "income" item, it will display in the "Age Variables" option list.
Here's the HTML markup:
<select multiple="" id="categories" name="categories[]">
<option class="category" value="income">income</option>
<option class="category" value="gender">gender</option>
<option class="category" value="age">age</option>
</select>
<select multiple="multiple" id="variables" name="variables[]">
<option class="income" value="10">$90,000 - $99,999</option>
<option class="income" value="11">$100,000 - $124,999</option>
<option class="income" value="12">$125,000 - $149,999</option>
<option class="income" value="13">Greater than $149,999</option>
<option class="gender" value="14">Male</option>
<option class="gender" value="15">Female</option>
<option class="gender" value="16">Ungendered</option>
<option class="age" value="17">Ages 18-24</option>
<option class="age" value="18">Ages 25-34</option>
<option class="age" value="19">Ages 35-44</option>
</select>
<select height="60" multiple="multiple" id="target" name="target[]">
</select>
And, here's the js:
/* This determines what options are display in the "Variables" select box */
var cat = $('#categories');
var el = $('#variables');
$('#categories option').click(function() {
var class = $(this).val();
$('#variables option').each(function() {
if($(this).hasClass(class)) {
$(this).show();
} else {
$(this).hide();
}
});
});
/* This adds the option to the target select box if the user clicks "add" */
$('#add').click(function() {
return !$('#variables option:selected').appendTo('#target');
});
/* This is the remove function in its current form, but doesn't append correctly */
$('#remove').click(function() {
$('#target option:selected').each(function() {
var class = $(this).attr('class');
if($('#variables option').hasClass(class)) {
$(this).appendTo('#variables');
sortList('variables');
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,您会发现您正走在错误的道路上,因为您无法在浏览器中一致地“隐藏”选项元素。您实际上需要添加和删除它们来更改列表。
我整理了您正在寻找的内容的工作版本,您可以查看它或在 JSBin 上编辑。
基本流程如下:
update_variables
函数,用于清空变量列表,添加所选类别的所有正确变量。这里重要的部分是,它检查目标是否已经具有该值的项目,并且不会再次添加它,因为它已经添加到目标中。#variables
中选定的元素附加到#target
并取消选择它们。#target
中删除所选元素并调用update_variables()
这里是一些方法的示例。查看 JSBin 上的源代码,了解所需的所有代码:
opts
最终看起来像这样:这是
update_variables
函数:注意:< code>option() 函数是一个为选项元素创建 HTML 的小助手。
You will actually find you are headed down the wrong road as you cannot "hide" option elements consistently across browsers. You actually need to add and remove them to alter the list.
I put together a working version of what you are looking for, and you can view it or edit it on JSBin.
The basic flow is as follows:
#variables
and store then in a hash/array for later use.update_variables
function that empties the variables list, adds in all the correct ones for the selected category. The important part here, is it checks if target has the item with that value already and will not add it again as it is already added to target.#variables
to#target
and deselect them.#target
and callupdate_variables()
Here is a sampling of a few methods. Look at the source on JSBin for all the code needed:
opts
ends up looking like this:Here is the
update_variables
function:Note: the
option()
function is a little helper that creates the HTML for an option element.假设您要移动的内容位于
$(this)
中,并且您希望将其附加到$(some_other_thing)
,则可以使用$(some_other_thing) .append($(this).html())
然后$(this).hide()
或$(this).remove()
。Assuming the thing you want to move is in
$(this)
, and you want to append it to$(some_other_thing)
, you might use$(some_other_thing).append($(this).html())
then$(this).hide()
or$(this).remove()
.看看 jQuery 1.4 detach() 方法..
我相信这就是你想要的。使用它和 appendTo() 你可以自由地将元素从一个元素移动到另一个元素。
Have a look at the jQuery 1.4 detach() method..
I believe it is what you want.. With it and appendTo() you can freely move elements around from one to another..