سؤال

وجديد إلى حد ما مسج، لذلك عذر محاولة لتحديد الأشياء بشكل صحيح.

وأنا بحاجة معرف عنصر لأن تنعكس في انتقاء التي هي وظائف زوجين عميق، وأنا لست متأكدا تماما كيفية التوجه نحو القيام بذلك.

$("input[type='file'][id^='pic_']").change(       //I need this element's id...
    function()
    {
        $("."+this.id).hide("fast",
            function()
            {
                //..to be reflected in this class selector
                $("."+this.id).attr("checked", true);
            }
        );
    }
);

وشكرا لمساعدتكم مقدما. : -)

هل كانت مفيدة؟

المحلول

ويستخدم هذا الإغلاق لجلب متغير مع هوية في نطاق وظيفة أعمق.

$("input[type='file'][id^='pic_']").change(       //I need this element's id...
        function()
        {
                var fileSelector = "." + this.id;
                $(fileSelector).hide("fast",
                        function()
                        {
                                //..to be reflected in this class selector
                                $(fileSelector).attr("checked", true);
                        }
                );
        }
);

نصائح أخرى

والمتغير this في رد الحدث هو عنصر DOM استضافة هذا الحدث. في رد التغيير وظيفة، وهذا هو العنصر تغييرها. لذلك:

$("input[type='file'][id^='pic_']").change(
  function() {
    $(this).hide("fast",
      function() {
        $(this).attr("checked", true);
      }
    }
  }
}

ويبدو أن مسألة نطاق المتغير:

$("#my_input").change(function() {
    var my_id = $(this).attr('id');
    $(this).hide('fast', function() {
        alert(my_id); // Will show an alert with the id from above.
        alert($(this).attr('id')); // *Should* show an alert with the same id.
    });
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top