-1

我确实有 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;
    });

});
4

2 回答 2

3

jQuery 可以很容易地根据元素的值、名称等过滤掉元素

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

上面的函数将只返回选择值为 5 的元素。

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

于 2013-04-25T02:48:12.070 回答
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);
};
于 2013-04-25T02:57:09.060 回答