0

我有这个简单的插件,但$(this).each()它不工作,它只调用一次而不是 onve 为每个选择器调用一次, $("input.[class~='checking']")我想为每个带有“检查”类的输入框调用一次,我完全迷失了至于为什么它不起作用

(function ($) {
    moSHclass = ''
    $.fn.isCheckedMO = function (moSHclass) {
        $("input.[class~='" + moSHclass + "']").hide();


        $(this).click(function () {
            var amChecked = 0

            $(this).each(function () {

                if (this.checked == true) {
                    alert('am:' + amChecked)
                    amChecked = amChecked + 1
                }
            });
            if (amChecked > 0) {

                $("input.[class~='" + moSHclass + "']").show();
            } else {
                $("input.[class~='" + moSHclass + "']").hide();
            }
        });
    }
})(jQuery);
4

3 回答 3

1

这已通过以下方式解决,我收到了很多回复,我非常感谢他们希望我将 $(this) 分配给一个变量,然后在 each() 运行中使用该变量而不是 $(this)。但是,有效的是这样做,然后将该变量放入 $(mySelector) 括号中,而不是代替整个部分。

(function($) {
moSHclass=''

$.fn.isCheckedMO=function(moSHclass) {
 $("input.[class~='"+moSHclass+"']").hide(); 

  $("td.[class~='"+moHTML+"']").addClass('red')


 var mySelector=$(this)
  $(this).click(function(){ 

var amChecked=0

    $(mySelector).each(function(){

  if (this.checked==true) { 

   amChecked=amChecked+1}
  });
if (amChecked>0) 
 {

 $("input.[class~='"+moSHclass+"']").show();
  }else { 
 $("input.[class~='"+moSHclass+"']").hide(); 
  }
 });
}
于 2013-07-26T15:32:23.600 回答
0

我敢打赌,您正试图访问each错误的this. 尝试:

(function ($) {
    moSHclass = ''
    $.fn.isCheckedMO = function (moSHclass) {
        var whatIThinkYouWantThisToBe = $("input.[class~='" + moSHclass + "']");
        whatIThinkYouWantThisToBe.hide(); 
        whatIThinkYouWantThisToBe.click(function () {
            var amChecked = 0
            //Store $(this) in $that for use within each callback...
            var $that = $(this);
            $that.each(function () {
                if (this.checked == true) {
                    alert('am:' + amChecked)
                    amChecked = amChecked + 1
                }
            });
            if (amChecked > 0) {

                $("input.[class~='" + moSHclass + "']").show();
            } else {
                $("input.[class~='" + moSHclass + "']").hide();
            }
        });
    }
})(jQuery);
于 2013-07-25T19:43:54.123 回答
0

为什么你不做

$("input.[class~='checking']").each(...

安装的

$(this).each(...

?

此外,您可以在控制台中进行调试,看看此时 $(this) 是什么

console.log( $(this) );

您可以通过这种方式保存 $(this) 以:

(function ($) {
moSHclass = ''
$.fn.isCheckedMO = function (moSHclass) {
    var $self = $(this);
    $("input.[class~='" + moSHclass + "']").hide();


    $(this).click(function () {
        var amChecked = 0

        $self.each(function () {

            if (this.checked == true) {
                alert('am:' + amChecked)
                amChecked = amChecked + 1
            }
        });
        if (amChecked > 0) {

            $("input.[class~='" + moSHclass + "']").show();
        } else {
            $("input.[class~='" + moSHclass + "']").hide();
        }
    });
}
})(jQuery);
于 2013-07-25T19:47:06.407 回答