简体   繁体   English

使用jQuery检测列表框中的值

[英]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, 但是,这里有问题,

  1. how do I know if listbox_destination still empty or has values ? 我怎么知道listbox_destination是否仍然为空或具有值?
  2. how do I know if there is no double value in listbox_destination when Sub District dropdown was selected for the second time and with the same value ? 我如何知道第二次选择“子区域”下拉列表时具有相同值的listbox_destination中是否没有双值?

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

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