[英]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 »</a>
<a href="JavaScript:void(0);" id="btn-remove">« 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();
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.