jQuery 序列化不包括隐藏字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1277824/
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
serialize doesn't include hidden fields
提问by gruszczy
I run serialize on a form, where on of the fields is hidden - and it's a very important field, which should be posted. Is there any way to easily serialize it through jQuery or should I write my own function?
我在表单上运行序列化,其中一个字段是隐藏的 - 这是一个非常重要的字段,应该发布。有什么方法可以通过jQuery轻松序列化它还是应该编写自己的函数?
回答by Josh Stodola
回答by Jim Schubert
Maybe combining the two in a single selector would work?
也许将两者结合在一个选择器中会起作用?
$(":input,:hidden").serialize();
edit: I just tried the above and it worked. but, $("form").serialize(); should automatically take all inputs as others have mentioned.
编辑:我刚刚尝试了上述方法并且有效。但是,$("form").serialize(); 应该像其他人提到的那样自动接受所有输入。
回答by Bob
Here's a weird variation on this problem. The hidden fields have names.
这是这个问题的一个奇怪的变化。隐藏字段有名称。
alert($("#myForm").serialize())
Correctly pops up a window with all of the hidden fields. But
正确弹出一个包含所有隐藏字段的窗口。但
$.post('myposts.php', $("#myForm").serialize(), function(data) {
$('#myResult').html(data);
});
Does not have the hidden fields. When the php script does print_r($_POST)
the hidden and checkboxes are mising
没有隐藏字段。当 php 脚本执行print_r($_POST)
隐藏和复选框丢失时
回答by KilleR
Just ran into this problem myself, and hacked out a solution.
刚刚自己遇到了这个问题,并找到了解决方案。
The problem has to do with the way JQuery picks up hidden html information. It will not pick up the TEXT of a hidden field as its value, you must use the value= property.
问题与 JQuery 获取隐藏 html 信息的方式有关。它不会选择隐藏字段的 TEXT 作为其值,您必须使用 value= 属性。
To set it in JQUERY use $(field).val(yourvalue);
要在 JQUERY 中设置它,请使用 $(field).val(yourvalue);
回答by SZL
You need set the name attribute and check the disabledattribute! The disabled field also not serialized.
您需要设置 name 属性并检查disabled属性!禁用字段也未序列化。
回答by Cengiz ?nkal
you should add name to all elements for serialize function to work properly
您应该为所有元素添加名称以使序列化功能正常工作
回答by Mark Tebault
I had this problem as well. Out of habit I close my input fields with />. I found that hidden input does not work when closed this way.
我也有这个问题。出于习惯,我用 /> 关闭了我的输入字段。我发现以这种方式关闭时隐藏输入不起作用。
<input type="hidden" name="someName" value="someValue" />
does not work.
不起作用。
<input type="hidden" name="someName" value="someValue" >
does work.
确实有效。