简体   繁体   English

不是jQuery选择器

[英]not jQuery selector

This Meteor client code tries to apply the jQuery .serializeArray on the form but not the elements which have the class inactive 这颗流星的客户端代码尝试应用了jQuery .serializeArray上的form但不具有类的元素inactive

const inputData = $('form:not[class="inactive"]').serializeArray();

Why is it failing and how to fix it? 为什么会失败以及如何解决? Thanks 谢谢

Fundamentally, to leave elements out of serializeArray , you need to make them disabled or remove their name . 从根本上讲,要将元素保留在serializeArray ,则需要将其禁用或删除其name So for instance: 因此,例如:

const inputData = $('form')
                      .find('.inactive')
                      .prop("disabled", true)
                      .end()
                      .serializeArray();

That: 那:

  • Finds all forms 查找所有表格
  • Finds the inactive elements in them 在其中找到不活动的元素
  • Ensures those elements are marked disabled 确保将这些元素标记为disabled
  • Returns to the forms ( .end() ) 返回表格( .end()
  • ...and serializes their elements. ...并序列化其元素。

Re the :not selector you tried to use, there were a few problems: 关于您尝试使用的:not选择器,有一些问题:

  1. You've used [] instead of () around the :not condition 您在:not条件周围使用了[]而不是()

  2. You should use a class selector, not an attribute selector, when using classes 使用类时,应使用类选择器,而不是属性选择器

  3. You were applying it to the form , not to elements within the form 你它是适用于该form ,而不是表单中的元素

But since you need to call serializeArray on the form , we need a different approach altogether (as above). 但是由于您需要在表单上调用serializeArray ,因此我们需要完全不同的方法(如上所述)。

When using a :not selector, keep the following things in mind: 使用:not选择器时,请记住以下几点:

  1. use () for :not instead of [] ()用作:not而不是[]
  2. don't select class as an attribute, use a class selector 不要选择class作为属性,使用类选择器
  3. as you want to select the elements inside the form there need to be a space between form and :not , otherwise you will only select all forms without .inactive class 因为要选择form的元素,所以必须在form:not之间留一个空格,否则,您将只选择all forms without .inactive class

This would be a correct element selector: 这将是正确的元素选择器:

const inputData = $('form :not(.inactive)');

To use serializeArray correctly, take a look at @TJCrowder's answer! 要正确使用serializeArray ,请查看@TJCrowder的答案!

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

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