jQuery $(this).find('input[type="submit"]:not(.cancel), button').click(function () - 这是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7745748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:14:02  来源:igfitidea点击:

$(this).find('input[type="submit"]:not(.cancel), button').click(function () - what is this?

jqueryjquery-selectors

提问by merk

Can someone help me understand this line of jquery code?:

有人可以帮我理解这行 jquery 代码吗?:

$(this).find('input[type="submit"]:not(.cancel), button').click(function ()

I'm particularly interested in .cancel, since the whole chunk of jquery code (not included here) is being used to validate a page and I'm trying to prevent the validation if the user clicks on the cancel button. I'm guessing the above line of code is saying it the submit button clicked is NOT the cancel button, then continue.

我对 尤其感兴趣.cancel,因为整个 jquery 代码块(此处未包含)都用于验证页面,并且我试图在用户单击取消按钮时阻止验证。我猜上面的代码行是说点击的提交按钮不是取消按钮,然后继续。

But I don't know how to designate a button as cancel.

但我不知道如何将按钮指定为取消。

回答by John Hartsock

Basically it is looking for elements located within thisthat has the following requirements

基本上它正在寻找位于this具有以下要求的元素

  1. is an input
  2. has type = submit
  3. does not have a class of cancel
  1. 是一个输入
  2. 有类型 = 提交
  3. 没有一类 cancel

OR

或者

  1. is a button
  1. 是一个按钮

回答by Ken Redler

Good answers here. Perhaps this annotated version adds some value.

好的答案在这里。也许这个带注释的版本增加了一些价值。

$(this)                   // within this jQuery object
  .find('                 // find all elements
    input[type="submit"]  // that are input tags of type "submit"
    :not(.cancel)         // and do not have the class "cancel"
    ,                     // or
    button                // are button elements
  ')
  .click(                 // and for each of these, to their click event
    function (){}         // bind this function
  );

回答by Brad Christie

They're applying a CSS class named cancel to symbolize the cancel button basically (then checking it's a button with out this class)

他们正在应用一个名为取消的 CSS 类来象征取消按钮(然后检查它是一个没有这个类的按钮)

Though I was un-aware you can use an asterisk (*) in a CSS class name.

尽管我不知道您可以*在 CSS 类名称中使用星号 ( )。

Nevermind, it was you trying to bold code within a code block

没关系,是你试图在代码块中加粗代码

I'm also curious why they make the cancel button of type submit when they want it to cancel anyways. (Seems like <input type="button" ... />would be a better fit).

我也很好奇他们为什么要取消提交类型的取消按钮,但无论如何都要取消它。(似乎<input type="button" ... />更合适)。

回答by Dennis

.cancelis a class selector. Specifically it is looking for inputelements with the type attribute set to submit, but don't have the cancelclass. It also looks for elements with the tagName button.

.cancel是一个类选择器。具体来说,它正在寻找inputtype 属性设置为submit,但没有cancel类的元素。它还查找带有 tagName 的元素button

You can "designate a button as cancel" like this:

您可以像这样“将按钮指定为取消”:

<input type='submit' class='cancel' />

回答by vittore

basically it is selector which says give me submit buttons without cancel class and buttons and apply click handler. try to look up the following selectors

基本上它是选择器,它说给我提交按钮而不取消类和按钮并应用点击处理程序。尝试查找以下选择器

tag .class :not

标签 .class : 不是