jQuery 排除序列化的某些输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4556172/
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-26 17:28:32  来源:igfitidea点击:

Excluding certain inputs on serialize

jqueryserialization

提问by helgatheviking

I am trying to exclude an input by name (it is a hidden input holding my nonce)

我正在尝试按名称排除输入(这是一个隐藏的输入,包含我的随机数)

The following question is almost what I am looking for:

以下问题几乎是我正在寻找的:

How do I use jQuery's form.serialize but exclude empty fields

我如何使用 jQuery 的 form.serialize 但排除空字段

but I have 2 questions about the solution there- which states that to serialize form data except for empty inputs and inputs where the value = "."

但我有 2 个关于那里的解决方案的问题 - 其中指出要序列化表单数据,除了空输入和值 = "." 的输入。

$("#myForm :input[value][value!='.']").serialize();

first of all, i can't get it to work with the jquery variable "this"

首先,我无法让它与 jquery 变量“this”一起使用

$('#ofform').live('submit', function(e) {
    e.preventDefault();
    var serializedReturn = $(this :input[name!='security']).serialize();        
});

And secondly I have a seperate form with the id of ofform-reset and if i use:

其次,我有一个单独的表单,id 为 ofform-reset,如果我使用:

var serializedReturn = $(#ofform :input[name!='security']).serialize(); 

it picks up the inputs in the other #ofform-reset form, AND/OR inputs that aren't enclosed within a tag.

它选取另一个 #ofform-reset 表单中的输入,以及未包含在标签中的 AND/OR 输入。

found the answer in one of my previous questions. invalid markup of the style:

在我之前的一个问题中找到了答案。样式的无效标记:

<form id="ofform">
 <div id="toolbar">
 <button id="save">Save</button>
</form>
<form id="ofform-reset">
 <button id="reset">Reset</button>
</form>
</div>

now to figure out how to use 2 different buttons to control the same form

现在来弄清楚如何使用 2 个不同的按钮来控制同一个表单

回答by prodigitalson

You don't need the :, because input is an element not a pseudo selector. Secondly you cannot use an object and a text string like that in your selector. You instead need to supply this as the scope argument to $():

您不需要:,因为输入是元素而不是伪选择器。其次,您不能在选择器中使用类似的对象和文本字符串。您需要将此作为范围参数提供给$()

$('#ofform').live('submit', function(e) {
    e.preventDefault();
    var serializedReturn = $('input[name!=security]', this).serialize();        
});

回答by jAndy

First, you need to invoke the .find()method like:

首先,您需要调用如下.find()方法:

var serializedReturn = $(this).find('input[name!=security]').serialize(); 

Otherwise the complete string would go into the css query engine (Sizzle).

否则,完整的字符串将进入 css 查询引擎 (Sizzle)。

Secondly:

其次:

I have another form with the id of ofform-reset and if i use:

我有另一个表单,id 为 ofform-reset,如果我使用:

You need to change this. It's invalid markup to have multiple ID's. If I understood you wrong here, the first solution might also help you here, invoking the .find()method aswell:

你需要改变这一点。具有多个 ID 的标记是无效的。如果我在这里理解错了,第一个解决方案也可以在这里帮助您,同时调用该.find()方法:

var serializedReturn = $('#ofform').find('input[name!=security]').serialize(); 

回答by user1429980

In my situation, this worked fine:

在我的情况下,这很好用:

$("fieldset").not(".tasks-container").serialize()

回答by VarunAgw

I am not happy with 'input[name!=security]'because it exclude all other input type like select,.. You can add them manually but this list just keep increasing with new HTML tags. So with every new tag coming, your code is broken again.

我不满意,'input[name!=security]'因为它排除了所有其他输入类型,如select,.. 您可以手动添加它们,但此列表只会随着新的 HTML 标签不断增加。因此,随着每个新标签的出现,您的代码再次被破坏。

Here is my solution:

这是我的解决方案:

$form.find(':not(input[name=draft])').serialize()

or

或者

$('form[name=expenses] :not(input[name=draft])').serialize()

Space is very important in second example.

在第二个例子中,空间非常重要。

回答by Devendra Rajput

Solved ! You should use the given solution to exclude input field to be serialized. It's tested and solved my problem.

解决了 !您应该使用给定的解决方案来排除要序列化的输入字段。它已经过测试并解决了我的问题。

var formdata = $($("#myform")[0].elements).not("#field_id").serialize();

for multiple fields, you can use the class name to exclude them.

对于多个字段,您可以使用类名来排除它们。

var formdata = $($("#myform")[0].elements).not(".class_name").serialize();

回答by Dimmduh

You can filter result array

您可以过滤结果数组

var ignoreFields = ["_csrf"];

$('form')
 .serializeArray()
 .filter(function(val){
    return ignoreFields.indexOf(val.name) === -1;
 });

Or shorter variant

或更短的变体

$('form').serializeArray().filter(val => ignoreFields.indexOf(val.name) === -1)