0

私はこの単純なプラグインを持っていますが、機能し$(this).each()ていません。セレクターごとに onve の代わりに 1 回しか呼び出しません $("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

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 に答える
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 に答える