jQuery jQuery从不是表单的元素序列化/ serializeArray
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9589126/
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
jQuery serialize / serializeArray from an element that is not a form
提问by B Z
I haven't found a concrete answer as to whether this is possible, but it seems like it should be...
我还没有找到关于这是否可能的具体答案,但似乎应该......
I would like to serialize all the input elements contained in a div. I can't use a form, because it would be nested within another form. I would then get the values and post them via ajax.
我想序列化 div 中包含的所有输入元素。我不能使用表单,因为它会嵌套在另一个表单中。然后我会获取值并通过ajax发布它们。
Here is the jsFiddle example I am playing with:
这是我正在玩的 jsFiddle 示例:
If I change the root to a it works as expected.
如果我将根更改为 a 它会按预期工作。
Thanks for your help.
谢谢你的帮助。
I've modified the jsfiddle from this other question:
我已经从另一个问题修改了 jsfiddle:
回答by Omer Bokhari
you need to serialize all the inputs inside your container, not the actual container itself. so:
您需要序列化容器内的所有输入,而不是实际的容器本身。所以:
$('div :input').serialize()
回答by tiru
Try this, to get all.
试试这个,得到所有。
$('#divID *').serialize()
回答by Guntram
this also works with
这也适用于
$('div :input').serializeArray()
:)
:)
回答by dishwasherWithProgrammingSkill
For serializing the contents of the div with a button, this will be the more efficient way as it doesn't traverse through the entire dom.
对于使用按钮序列化 div 的内容,这将是更有效的方式,因为它不会遍历整个 dom。
$(this).closest('div').find("input,select,textarea").serialize();
$(this).closest('div').find("input,select,textarea").serialize();
make sure to attach this with the button event and make sure to include event.preventdefaultwith the button so that it does not submit the primary form if the div is inside it.
确保将其与按钮事件附加在一起,并确保将event.preventdefault包含在按钮中,这样如果 div 在其中,它就不会提交主表单。