如何查找具有所选用户值的组合框

时间:2013-04-25 02:32:21

标签: jquery

我确实有5个同组的组合框。

我想找到哪个组合框中选择了值5

我的尝试:

$(document).ready(function($){
    previous = 0;
    $('.check').focus(function () {
        // Store the current value on focus and on change
        previous = this.value;
    }).change(function() {
        // Do something with the previous value after the change
        //change previous container with value
        newVal = this.value; 
        //alert(previous);
        console.log('Old Val: ' + previous + '- new val:' + newVal);
        $lst = $('.ranking option[value=' + newVal + ']:selected');
        console.log('one ' + $lst.length);

        // Make sure the previous value is updated
        previous = this.value;
    });

});

2 个答案:

答案 0 :(得分:3)

jQuery可以根据元素的名称,名称等过滤掉元素

$('.check').filter(function(){
  return $(this).find('option:selected').val() == 5;
})

上述函数将仅返回仅选择值5的select元素。

这是小提琴http://jsfiddle.net/N2bya/

答案 1 :(得分:0)

您好,您可以查看我的jsFiddle完成所需的所有代码。

这是整个代码的片段:

$('select').change(function (){   //trigger for the change event of the comboboxes

    var selectedIndex = $(this).prop("selectedIndex");
    if(selectedIndex === 4) { //basically the 5th item as it is zero indexed
        alert('you have chosen the fifth item');
    }
});


$('#checker').click(function (){   //for checking of all items with item number 5 selected
   indexChecker(); 
});

var indexChecker = function (){
    var controlList =[];
    $('select').each(function (){
        if($(this).prop("selectedIndex") === 4){
            controlList.push($(this).attr('id'));

        }
    });
    alert('You have selected the fifth element in <select> element with id of : '+ controlList);
};