jQuery SerializeArray 和 Not 选择器
jQuery SerializeArray and Not selector
我正在使用 .serializeArray 函数收集表单的所有输入,但我想排除某个 div class 内的输入。我认为 .not() 选择器会执行此操作,但它似乎不起作用。
最基本的代码是我的代码:
HTML:
<form class="theForm">
<input name="a" value="a"/>
<div class="excludeMe">
<input name="c" value="c"/>
</div>
</form>
Javascript:
$.each($(".theForm").not(".excludeMe").serializeArray(),
function() {
alert(this.name);
});
我希望警报只是 'a',但这是警报 'a',然后是 'b'。
Fiddle: https://jsfiddle.net/cysaczu5/
我们会在输入而不是 div 上使用这个 class,它会起作用。
'
$.each($("input").not(".excludeMe").serializeArray(),
function() {
alert(this.name);
});
'
<input name="c" class='excludeMe' value="c"/>
您必须将所有输入包装在 <div><input/></div>
中
然后将选择器改成这个$.each($(".theForm :not(.excludeMe) input")
https://jsfiddle.net/cysaczu5/3/
我建议删除 div 并直接将 "excludeMe" class 添加到输入中。那么选择器就变成了$.each($(".theForm input:not(.excludeMe)")
请尝试
<form class="theForm">
<input name="a" value="a"/>
<div class="excludeMe">
<input class="excludeMe" name="c" value="c"/>
</div>
</form>
$.each($(".theForm :not(.excludeMe)").serializeArray(),
function() {
alert(this.name);
});
最简单的答案是从要跳过的输入中删除 name
属性。这样它也被 serializeArray()
:
跳过了
HTML:
<form class="theForm">
<input name="a" value="a" />
<input value="c" />
</form>
JS:
$.each($(".theForm").serializeArray(), function() {
alert(this.name);
});
只需将 input
放在 :not
语句之前以仅匹配表单内的输入,而不匹配表单本身:
$.each($(".theForm input:not(.excludeMe)").serializeArray(), function() {
alert(this.name);
});
我正在使用 .serializeArray 函数收集表单的所有输入,但我想排除某个 div class 内的输入。我认为 .not() 选择器会执行此操作,但它似乎不起作用。
最基本的代码是我的代码:
HTML:
<form class="theForm">
<input name="a" value="a"/>
<div class="excludeMe">
<input name="c" value="c"/>
</div>
</form>
Javascript:
$.each($(".theForm").not(".excludeMe").serializeArray(),
function() {
alert(this.name);
});
我希望警报只是 'a',但这是警报 'a',然后是 'b'。
Fiddle: https://jsfiddle.net/cysaczu5/
我们会在输入而不是 div 上使用这个 class,它会起作用。
'
$.each($("input").not(".excludeMe").serializeArray(),
function() {
alert(this.name);
});
'
<input name="c" class='excludeMe' value="c"/>
您必须将所有输入包装在 <div><input/></div>
然后将选择器改成这个$.each($(".theForm :not(.excludeMe) input")
https://jsfiddle.net/cysaczu5/3/
我建议删除 div 并直接将 "excludeMe" class 添加到输入中。那么选择器就变成了$.each($(".theForm input:not(.excludeMe)")
请尝试
<form class="theForm">
<input name="a" value="a"/>
<div class="excludeMe">
<input class="excludeMe" name="c" value="c"/>
</div>
</form>
$.each($(".theForm :not(.excludeMe)").serializeArray(),
function() {
alert(this.name);
});
最简单的答案是从要跳过的输入中删除 name
属性。这样它也被 serializeArray()
:
HTML:
<form class="theForm">
<input name="a" value="a" />
<input value="c" />
</form>
JS:
$.each($(".theForm").serializeArray(), function() {
alert(this.name);
});
只需将 input
放在 :not
语句之前以仅匹配表单内的输入,而不匹配表单本身:
$.each($(".theForm input:not(.excludeMe)").serializeArray(), function() {
alert(this.name);
});