简体   繁体   English

如何使用关键字$(this)代替类选择器-jQuery?

[英]How can I use the keyword $(this) in place of class selector - jQuery?

I have the following code in jQuery - 我在jQuery中有以下代码-

$(".new_makes_button").click(function(){
    if($(".new_makes_button:checked").length == 0) {

So how can I replace class selector ".new_makes_button" in second line of above code with $(this)? 那么,如何用$(this)替换上面代码第二行中的类选择器“ .new_makes_button”? I am just in search of correct syntax. 我只是在寻找正确的语法。 Here I have several checkboxes with class "new_makes_button" and clicking on any of it, I am checking when all the checkboxes will be unchecked then run the following code. 在这里,我有几个带有“ new_makes_button”类的复选框,然后单击其中的任何一个,我正在检查何时所有复选框都未选中,然后运行以下代码。

Try this: 尝试这个:

if (!$(this).is(":checked")) {
    ...
}

You can just look at the checked property directly on the clicked on item like this: 您可以直接在单击的项目上查看选中的属性,如下所示:

if (!$(this).prop("checked")) {
    // code here for when the item is not checked
}

Once you have the DOM element (which you have in this ), there is no need to use further selectors as you can just examine the properties of that item directly. 一旦有了DOM元素(在this元素中),就无需使用其他选择器,因为您可以直接检查该项目的属性。

This will check if the current item, is checked. 这将检查当前项目是否被选中。 The current item is the Event source element. 当前项目是事件源元素。 $(this) inside the callback is a reference to a single element. 回调中的$(this)是对单个元素的引用。

if(this.checked === false) {

}

But, if you want to find out if none of the elements with .new_makes_button are checked then you can use jQuery. 但是,如果要查找是否.new_makes_button选中.new_makes_button的所有元素,则可以使用jQuery。

$(".new_makes_button").click(function() {
   // $(this) at this point is a reference to a 
   // single element. To get Them all you may do this:
   var total = 0;
   $(".new_makes_button").each(function(index, element) {
     if(this.checked === true) {
       total++;
     }
   });
   alert("Total checked item" + (total).toString());
});

example

how can I replace class selector ".new_makes_button" in second line of above code with $(this)? 如何用$(this)替换上面代码第二行中的类选择器“ .new_makes_button”?

If you really want to use this object, then you can, however, it isn't not correct syntax to not use $(this) . 如果您确实要使用this对象,则可以使用不使用$(this)语法,这不是正确的语法。

To use this inside that method and have it reference an array of .new_makes_button elements, you can change it's value using bind . 要在该方法中使用this方法并使其引用.new_makes_button元素数组,可以使用bind更改其值。 Here in this example, this will reference a jQuery object. 在此示例中, this将引用一个jQuery对象。

$(".new_makes_button").click(function(e) {
  // here `this` is a jquery object!
  // no need to call the `$`
  console.log(this.length);
}.bind($(".new_makes_button")));

example

Shortest possible solution 最短的解决方案

$(".new_makes_button").click(function(e) {
  if(this.filter(':checked').length == 0) {
     // handle condition        
   }
}.bind($(".new_makes_button")));

In jQuery, the element being acted upon is often passed to the function parameter of a jQuery function as this. 在jQuery中,要作用的元素通常以此方式传递给jQuery函数的函数参数。 It can be made into a jQuery element object by using$(this) in the function parameter, ex: 可以通过在函数参数中使用$(this)将其制成jQuery元素对象,例如:

$('.new_makes_button').click(function(){ $( 'new_makes_button')。点击(函数(){

$(this).animate({paddingLeft: $(this).width()}, 200); $(this).animate({paddingLeft:$(this).width()},200);

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM