[英]Detecting value in listbox with jQuery
I have a dropdown box (Sub District) and a "listbox_source" (villages). 我有一个下拉框(Sub District)和一个“ listbox_source”(村庄)。 I prepare value for the dropdown and listbox from sql server and listbox's values depend on drop down, dynamic.
我从SQL Server为下拉列表和列表框准备值,列表框的值取决于动态下拉列表。
Then, I have another "listbox_destination" for moving selected village from "listbox_source". 然后,我还有另一个“ listbox_destination”,用于将选定的村庄从“ listbox_source”中移出。 I've done for moving value from listbox_source to listbox_destination .
我已经完成了将值从listbox_source移到listbox_destination的工作 。
but, then, there is questions, 但是,这里有问题,
Please, advice... 请指教...
I try give an example of my code: Dropdown: 我尝试举一个示例代码:Dropdown:
<select id="ID_Villages" size="10" multiple="">
<option value="42">Bandul</option>
<option value="43">Dedap</option>
<option value="44">Mekar Delima</option>
<option value="45">Putri Puyu</option>
<option value="46">Tanjung Padang</option>
<option value="47">Tanjung Pisang</option>
<option value="187">Kudap</option>
<option value="188">Selat Akar</option>
<option value="189">Mengkopot</option>
<option value="190">Mengkirau</option>
$(document).ready(function (e) {
$('#btn_To_Right').click(function (e) {
var selectedList = $('#ID_Villages option:selected').toArray();
if ($('selectedList').length == 0) {
alert("Nothing to move.");
e.preventDefault();
} else {
<!-- #1. how to check listbox_destination still empty or has values ? -->
$(selectedVillage).append($(selectedList).clone());
$(selectedList).remove();
alert(selectedVillage.length);
} else {
<!-- #2. how to avoid double values -->
$each(selectedList, function (index, value) {
if($selectedVillage ??? ).length == 0){
<!-- add new value -->
} else {
alert ("data already exist");
}
}
}
}
})
})
UPDATE for first moving ( #1 ), from listbox_source to listox_destination , I check listox_destination by using 更新第一移动(#1),从listbox_source到listox_destination,我通过使用检查listox_destination
if ($("#lst_ID_Desa_Selected option").length == 0) {
$(selectedVillage).append($(selectedList).clone());
$(selectedList).remove();
}
and then for double values protection ( #2 ) 然后进行双值保护( #2 )
$.each(selectedList, function (index, value) {
alert($("selectedList option[value='" + 42 + "']").length);
alert($("selectedList option[value='" + value.value + "']"));
alert($("selectedList <option value='" + value.value + "'>"));
})
I tried to get value from 我试图从中获取价值
alert($("selectedList option[value='" + value + "']").length);
by using alert but popup value appears "0". 通过使用警报,但弹出值显示为“ 0”。 Whereas, selectedList is the source data of villages.
而selectedList是村庄的源数据。
I tried to get value from 我试图从中获取价值
alert($("selectedList option[value='" + value.value + "']"));
by using alert and the return value as [object Object] 通过使用alert和返回值作为[object Object]
while I tried to get value from 当我试图从中获取价值
alert($("selectedList <option value='" + value.value + "'>"));
there is error message: 有错误信息:
syntax error, unrecognized expression: selectedList <option value='42'>
I think, I have to check selectList syntax before I use it for if under $.each syntax. 我想,我有之前我用它,如果下。每$语法检查选择列表的语法。
Please, I need further advice for this JS/jQuery syntax. 请,我需要有关此JS / jQuery语法的更多建议。
To find whether list is empty or not, is can be done using the following code after $(selectedList).remove();
要查找列表是否为空,可以在
$(selectedList).remove();
之后使用以下代码来完成$(selectedList).remove();
$("#ID_Villages option").length; //If 0 then no options in the select list
To check for duplicates, you can use something like: 要检查重复项,可以使用以下方法:
$("selectedDesa option[value='42']").length;
If the value of above is 0
then no option found in the destination list. 如果上面的值为
0
则在目标列表中找不到任何选项。 If the length is 1
then option already exists. 如果长度为
1
则选项已经存在。 You can replace the value
attribute based on the selected option value from the source list. 您可以根据从源列表中选择的选项值来替换
value
属性。
Update: 更新:
There is no need to loop. 无需循环。 You can use the logic as below:
您可以使用以下逻辑:
var selectedList = $('#ID_Villages option:selected').toArray();
var selectedVal = $('#ID_Villages option:selected').val(); // New code
Then while checking for duplicates, use the below: 然后在检查重复项时,使用以下方法:
$("selectedDesa option[value='" + selectedVal + "']").length;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.