如何获取除$(this)以外的所有类值

时间:2014-11-24 12:00:42

标签: javascript jquery dom

我正在尝试遍历class entry_percent中的所有滑块值,不包括当前选定的滑块。

无论我如何尝试,我都无法过滤掉当前选定的滑块?

我错过了什么......

这是代码...... (注释掉了我尝试过的一些内容)....

// get total of sliders other then current
        function sum_sliders() {
            var sum=0;
            var total=0;

            //var sliders = $('.entry_percent:not(this)');// get every slider other then the current

            //var sliders = $('.entry_percent').not(this);

            //var sliders = $('.entry_percent:not(this)');

            //var sliders = $('.entry_percent:not(this)'

            // var sliders = $('.entry_percent:not').(this);

            var sliders = $('.entry_percent').not(this);


            //iterate through each input and add to sum
            $(sliders).each(function() {

                sum += parseFloat(this.value);
                console.log('Sum sliders value: '+ sum);

            });

            return(sum);

        }

1 个答案:

答案 0 :(得分:1)

您正在寻找的地方没有this。您必须将this传递给函数,如下所示:

function sum_sliders() {
     var sum=0, total=0;
     var sliders = $('.entry_percent').not(this);
     //iterate through each input and add to sum
     $(sliders).each(function() {
         sum += parseFloat(this.value);
         console.log('Sum sliders value: '+ sum);
     });
     return(sum);
 }
 $("#elementYouClickToCallFunction").click(sum_sliders); //Instead of using an anonymous function

还有 - 根据您提供的内容 - 没有理由不将sum_sliders的内容简单地包含在点击绑定的匿名函数中:

$("#elementYouClickToCallFunction").click(function(){
     var sum=0, total=0;
     var sliders = $('.entry_percent').not(this);
     //iterate through each input and add to sum
     $(sliders).each(function() {
         sum += parseFloat(this.value);
         console.log('Sum sliders value: '+ sum);
     });
     return(sum);
 });