排除序列化上的某些输入

hel*_*ing 40 jquery serialization

我试图按名称排除输入(它是一个隐藏的输入,保存我的随机数)

以下问题几乎就是我要找的:

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

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

$("#myForm :input[value][value!='.']").serialize();
Run Code Online (Sandbox Code Playgroud)

首先,我无法使用jquery变量"this"

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

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

var serializedReturn = $(#ofform :input[name!='security']).serialize(); 
Run Code Online (Sandbox Code Playgroud)

它接收其他#ofform-reset形式的输入,AND/OR输入未包含在标记内.

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

<form id="ofform">
 <div id="toolbar">
 <button id="save">Save</button>
</form>
<form id="ofform-reset">
 <button id="reset">Reset</button>
</form>
</div>
Run Code Online (Sandbox Code Playgroud)

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

pro*_*son 43

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

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


jAn*_*ndy 12

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

var serializedReturn = $(this).find('input[name!=security]').serialize(); 
Run Code Online (Sandbox Code Playgroud)

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

其次:

我有另一个形式,其id为ofform-reset,如果我使用:

你需要改变它.拥有多个ID的无效标记.如果我在这里理解你错了,第一个解决方案也可以在这里帮助你,调用.find()方法以及:

var serializedReturn = $('#ofform').find('input[name!=security]').serialize(); 
Run Code Online (Sandbox Code Playgroud)


use*_*980 8

在我的情况下,这工作正常:

$("fieldset").not(".tasks-container").serialize()
Run Code Online (Sandbox Code Playgroud)

  • 不适用于我,jAndy的解决方案适用于我 (3认同)

Var*_*Agw 6

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

这是我的解决方案:

$form.find(':not(input[name=draft])').serialize()
Run Code Online (Sandbox Code Playgroud)

要么

$('form[name=expenses] :not(input[name=draft])').serialize()
Run Code Online (Sandbox Code Playgroud)

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