简体   繁体   English

如何在两个列表框之间移动项目?

[英]How to move items between two listboxes?

    <div class="data_wrap list-padding">
    <div class="approveTermsContainer">
        <div class="newItems"> <b>send</b>
            <br />
            <select multiple="multiple" id='lstBox1'>
                    <ul class="tree" multiple="multiple" id='lstBox1'>
                <li>
                    <div class="btn-group" data-toggle="buttons"> </div> <a href="">heading test</a>
                    <ul>
                        <li>
                            <div class="btn-group" data-toggle="buttons"> </div> test <span rel="tooltip" data-toggle="tooltip" data-trigger="hover" data-placement="right" data-html="true" data-title="This is my tooltip!"><i class="fa fa-info-
circle"></i></span> </li>
                    </ul>
                </li>
            </ul>
      </select>
        </div>
        <div class="transferBtns">
            <input type='button' id='btnRight' value='  >  ' />
            <br />
            <input type='button' id='btnLeft' value='  <  ' /> </div>
        <div class="approvedItems"> <b>receive</b>
            <br />
            <select multiple="multiple" id='lstBox2'> </select>
        </div>
    </div>
</div>

In the above code, i have the two listboxes.在上面的代码中,我有两个列表框。 ie, listbox1, listbox2.即,listbox1,listbox2。 Where On selecting the listboxes1 item(heading test), and selecting the btn.在哪里选择 listboxes1 项目(标题测试),然后选择 btn。 it has to move to the listbox2.它必须移动到listbox2。

$(document).ready(function () {
  $('#btnRight').click(function (e) {
    var selectedOpts = $('#lstBox1 option:selected');
    if (selectedOpts.length == 0) {
      alert("Nothing to move.");
      e.preventDefault();
    }

    $('#lstBox2').append($(selectedOpts).clone());
    $(selectedOpts).remove();
    e.preventDefault();
  });

  $('#btnLeft').click(function (e) {
    var selectedOpts = $('#lstBox2 option:selected');
    if (selectedOpts.length == 0) {
      alert("Nothing to move.");
      e.preventDefault();
    }

    $('#lstBox1').append($(selectedOpts).clone());
    $(selectedOpts).remove();
    e.preventDefault();
  });
});

Above is the jquery code, which I have tried.上面是jquery代码,我试过了。 Am i attaching my codepen code here.我在这里附上我的codepen代码吗?

https://codepen.io/dhanunjayt/pen/GRxMpBX https://codepen.io/dhanunjayt/pen/GRxMpBX

Your JS code is right, and also works.您的 JS 代码是正确的,并且也有效。

You should change your HTML in select element.您应该更改 select 元素中的 HTML 。

Here is some code works demo .这是一些代码作品演示

<select multiple id='lstBox1'>
   <option value="1">foo</option>
   <option value="2">bar</option>
</select>

In the HTML code, the first SELECT element has one child, an UL element.在 HTML 代码中,第一个 SELECT 元素有一个子元素,即 UL 元素。 As far as I know, the children of SELECT elements are OPTION elements.据我所知,SELECT 元素的子元素是 OPTION 元素。 You should not have an UL element inside a SELECT element. SELECT 元素内不应有 UL 元素。

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select : https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select

Each menu option is defined by an element nested inside the <select>.每个菜单选项都由嵌套在 <select> 中的元素定义。 [...] You can further nest <option> elements inside elements to create separate groups of options inside the dropdown. [...] 您可以在元素内进一步嵌套 <option> 元素,以在下拉菜单中创建单独的选项组。

Besides you have different elements with the same ID and there aren't OPTION elements in the code.此外,您有具有相同 ID 的不同元素,并且代码中没有 OPTION 元素。

Probably $('#lstBox1 option:selected');可能$('#lstBox1 option:selected'); is not working as you expected because:没有按预期工作,因为:

  • the elements <select multiple="multiple" id='lstBox1'> and <ul class="tree" multiple="multiple" id='lstBox1'> have the same ID ( lstBox1 );元素<select multiple="multiple" id='lstBox1'><ul class="tree" multiple="multiple" id='lstBox1'>具有相同的 ID ( lstBox1 );
  • there aren't OPTION elements in the HTML code ( option:selected never matches); HTML 代码中没有 OPTION 元素( option:selected从不匹配);

When I try to validate the HTML , I get those error messages (besides others):当我尝试验证 HTML时,我收到了这些错误消息(除了其他错误消息):

Duplicate ID lstBox1.重复 ID lstBox1。

From line 6, column 12;从第 6 行第 12 列开始; to line 6, column 61到第 6 行第 61 列

 <ul class="tree" multiple="multiple" id='lstBox1'>

[...] [...]

Stray start tag ul.杂散开始标记 ul。

From line 6, column 12;从第 6 行第 12 列开始; to line 6, column 61到第 6 行第 61 列

 ul class="tree" multiple="multiple" id='lstBox1'>

[...] [...]

Text not allowed in element select in this context.在此上下文中,元素 select 中不允许出现文本。

From line 8, column 70;从第 8 行第 70 列开始; to line 8, column 81到第 8 行第 81 列

<a href="">heading test</a>

Content model for element select: Zero or more option, optgroup, and script-supporting elements.元素 select 的内容 model:零个或多个选项、optgroup 和脚本支持元素。

Change改变

The UL element is not visible inside the SELECT element, at least in Firefox. UL 元素在 SELECT 元素内部不可见,至少在 Firefox 中是不可见的。

Changing the UL list to OPTION elements, the options are visible and you can move them without changing the jQuery code.将 UL 列表更改为 OPTION 元素,选项是可见的,您可以在不更改 jQuery 代码的情况下移动它们。

<div class="data_wrap list-padding">
    <div class="approveTermsContainer">
        <div class="newItems"> <b>send</b>
            <br />
        <select multiple="multiple" id='lstBox1'>
            <optgroup label="heading test">
                <option value="test">test</option>
            </optgroup>
        </select>
        </div>
        <div class="transferBtns">
            <input type='button' id='btnRight' value='  >  ' />
            <br />
            <input type='button' id='btnLeft' value='  <  ' /> </div>
        <div class="approvedItems"> <b>receive</b>
            <br />
            <select multiple="multiple" id='lstBox2'> </select>
        </div>
    </div>
</div>

I used this logic to do it and it worked well for me, just appending string to select "<option>your selected option</option>"我使用这个逻辑来做到这一点,它对我来说效果很好,只需将字符串附加到 select "<option>your selected option</option>"

<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>     
    </head>
    <body>
       <select id="l1">
        <option value="1">1</option>
        <option value="2">2</option>
       </select>
       <input type="button" id="btn" value="click">
       <select id="l2"></select>
    </body>
</html>
<script>
    $("#btn").click(function(){
        debugger;
        value = $("#l1").find(":selected").attr('value');
        var appends='<option value="' +value + '">' + value + '</option>';
        $("#l2").empty();
        $("#l2").append(appends);
    })
 </script>

i have checked your code it is working, just remove <optgroup> and <li> , make it simple like我检查了你的代码它正在工作,只需删除<optgroup><li> ,让它变得简单

<select multiple="multiple" id='lstBox1'>
     <option value="test">test</option>
     <option value="test2">test2</option>
     <option value="test3">test3</option>
</select>

You can achieve what you're looking for.你可以实现你正在寻找的东西。 You need to make a few changes.您需要进行一些更改。

Here is an example of how it should be structured and how it should behave.这是一个应该如何构建以及应该如何表现的示例。

<select multiple id="list_1">
    <option id="option_a">Option A</option>
    <option id="option_b">Option B</option>
    <option id="option_c">Option C</option>
</select>
<select multiple id="list_2">
    //
</select>
<script>
    function clickRight() {
        var list = document.querySelector("#list_1").children;
        var numOptions = list.length;
        for (var i = 0; i < numOptions; i++) {
            var option = list[i];
            if (option.selected) {
                document.querySelector("#list_2").appendChild(list.splice(i, 1));
                numOptions—;
                i—;
            }
        }
    }
</script>

You would need to write a clickLeft function as well.您还需要编写clickLeft function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM