jquery 删除选定的元素并附加到另一个元素

发布于 2024-08-25 06:27:11 字数 2666 浏览 6 评论 0原文

我正在尝试将“已删除的选项”重新附加到适当的选择选项菜单。

我有三个选择框:“类别”、“变量”和“目标”。 “类别”是一个链接选择,因此当用户从中选择一个选项时,“变量”选择框将填充特定于所选类别选项的选项。当用户从“变量”选择框中选择一个选项时,它将附加到“目标”选择框。我有一个“删除选定的”功能,这样如果用户从“目标”选择框中“删除”选定的元素,它就会从“目标”中删除并放回到“变量”选项池中。我遇到的问题是它不加区别地将选项附加到“变量”项。也就是说,如果所选类别是“年龄”,则“变量”选项都具有“年龄”类别。但是,如果删除的选项是“收入”项目,它将显示在“年龄变量”选项列表中。

这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扛刀软妹 2024-09-01 06:27:11

实际上,您会发现您正走在错误的道路上,因为您无法在浏览器中一致地“隐藏”选项元素。您实际上需要添加和删除它们来更改列表。

我整理了您正在寻找的内容的工作版本,您可以查看它在 JSBin 上编辑

基本流程如下:

  1. 页面加载时,从#variables 中获取所有选项并将其存储在哈希/数组中以供以后使用。
  2. 创建一个 update_variables 函数,用于清空变量列表,添加所选类别的所有正确变量。这里重要的部分是,它检查目标是否已经具有该值的项目,并且不会再次添加它,因为它已经添加到目标中。
  3. 在“添加”中,只需将 #variables 中选定的元素附加到 #target 并取消选择它们。
  4. 在“删除”时,只需从 #target 中删除所选元素并调用 update_variables()

这里是一些方法的示例。查看 JSBin 上的源代码,了解所需的所有代码:

opts 最终看起来像这样:

var opts = {
    'age':[
       { label: 'Ages 18-24', value: 17 },
       { label: 'Ages 25-34', value: 18 },
       { label: 'Ages 35-44', value: 19 }
    ]
    ....
}

这是 update_variables 函数:

function update_variables(){
   var cat = $cats.val(), new_opts = [];
   $vars.empty();

   $.each(opts[cat], function(){
     if( $target.find('[value=' + this.value + ']').length === 0 ){
       new_opts.push(option(this.value, this.label));
     }
   });

   $vars.html(new_opts.join(''));
}

注意:< 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:

  1. On page load, grab all the options from #variables and store then in a hash/array for later use.
  2. Create a 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.
  3. On "Add" just append the selected elements from #variables to #target and deselect them.
  4. On "Remove" just remove the selected elements from #target and call update_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:

var opts = {
    'age':[
       { label: 'Ages 18-24', value: 17 },
       { label: 'Ages 25-34', value: 18 },
       { label: 'Ages 35-44', value: 19 }
    ]
    ....
}

Here is the update_variables function:

function update_variables(){
   var cat = $cats.val(), new_opts = [];
   $vars.empty();

   $.each(opts[cat], function(){
     if( $target.find('[value=' + this.value + ']').length === 0 ){
       new_opts.push(option(this.value, this.label));
     }
   });

   $vars.html(new_opts.join(''));
}

Note: the option() function is a little helper that creates the HTML for an option element.

超可爱的懒熊 2024-09-01 06:27:11

假设您要移动的内容位于 $(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().

云淡风轻 2024-09-01 06:27:11

看看 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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文