简体   繁体   English

如何在jQuery中添加/删除下拉多选列表项

[英]How to add/remove dropdown multiselect list items in jquery

I am using a multiselect dropdown list and filled items from database like below, it works fine the first time, but based on selections from other dropdowns, I need to remove values and add other values. 我正在使用多选下拉列表并从数据库中填充项目,如下所示,它第一次运行良好,但是基于其他下拉列表中的选择,我需要删除值并添加其他值。

$('#PID').multiselect({
    columns: 1,
    placeholder: 'Select project'
});

I am unable to modify the dropdown's values, can anybody help me? 我无法修改下拉菜单的值,有人可以帮我吗?

I referred to the link below. 我提到了下面的链接。

http://www.codexworld.com/multi-select-dropdown-list-with-checkbox-jquery/ http://www.codexworld.com/multi-select-dropdown-list-with-checkbox-jquery/

I tried something like the following: 我尝试了以下内容:

$("#PID").append('<option value="option5">Option ' + ++count + '</option>');
$("#PID").multiselect('refresh');

Demo link http://www.designchemical.com/blog/index.php/jquery/create-add-remove-select-lists-using-jquery/ 演示链接http://www.designchemical.com/blog/index.php/jquery/create-add-remove-select-lists-using-jquery/

<form>
  <fieldset>

    <select name="selectfrom" id="select-from" multiple size="5">
      <option value="1">Item 1</option>
      <option value="2">Item 2</option>
      <option value="3">Item 3</option>
      <option value="4">Item 4</option>
    </select>

    <a href="JavaScript:void(0);" id="btn-add">Add &raquo;</a>
    <a href="JavaScript:void(0);" id="btn-remove">&laquo; Remove</a>

    <select name="selectto" id="select-to" multiple size="5">
      <option value="5">Item 5</option>
      <option value="6">Item 6</option>
      <option value="7">Item 7</option>
    </select>

  </fieldset>
</form>

The JQuery Code jQuery代码

$(document).ready(function() {

    $('#btn-add').click(function(){
        $('#select-from option:selected').each( function() {
                $('#select-to').append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
            $(this).remove();
        });
    });
    $('#btn-remove').click(function(){
        $('#select-to option:selected').each( function() {
            $('#select-from').append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
            $(this).remove();
        });
    });

});

You can unload multiselect dropdown using below code and reinitialize it: 您可以使用以下代码卸载多选下拉列表并重新初始化:

$('#PID').multiselect( 'unload' );

You can also reload using below code: 您还可以使用以下代码重新加载:

$('#PID').multiselect( 'reload' );

Please check here for more options. 在此处查看更多选项。

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

相关问题 如何从Jquery中的多选下拉列表中删除选项 - How to remove Options from multiselect dropdown in Jquery jQuery jTable-如何添加多选下拉列表? - Jquery jTable - how to add multiselect dropdown? 如何在胡子中使用jQuery multiselect同时显示组名和下拉列表项 - How to display Group name also along with dropdown list items using jquery multiselect in mustache 如何使用jQuery multiselect同时显示组名和下拉列表项? - How to display Group name also along with dropdown list items using jquery multiselect? 使用jQuery向Kendo Multiselect添加项目 - Add items to Kendo Multiselect with jQuery jQuery multiselect下拉列表,包含所选项目的关闭按钮 - jQuery multiselect dropdown with close button for selected items 如何使用jQuery从多选下拉列表中打印选定的值? - how to print selected values from multiselect dropdown list using jquery? 如何在单击 jQuery 中的列表项时向 div 添加和删除多个值? - How to add & remove multiple values to div on click of list items in jQuery? 如何在多 select 引导下拉列表 jquery 中选择 2 个下拉列表的基础上添加/删除列表中的元素? - How to add/remove element in list on the basis of selection of 2 dropdown list in multi select bootstrap dropdown jquery? 使用jquery从下拉列表中删除特定项 - remove specific items from dropdown list using jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM