序列化表单在 jQuery 中不起作用

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

Serialize form not working in jQuery

jquery

提问by Nikola

Can you please take a look and help me realize where am I going wrong with this? Here is the jsfiddle link: http://jsfiddle.net/Hitman666/QcEkj/1/but also here is that code

你能看一下并帮助我意识到我哪里出了问题吗?这是 jsfiddle 链接:http: //jsfiddle.net/Hitman666/QcEkj/1/但这里也是代码

HTML:

HTML:

<form action="#" id="gamesForm">
    <p>                                                        
        <input id="gName" type="text" class="medium" />
        <span class="notification information">Game name</span>
    </p>

    <p>                            
        <span class="notification information">Enabled:</span>
        <input id="gEnabled" type="checkbox" />              
    </p>

    <br />
    <!--Additional data for extra type-->
    <div id="extraAdditionalData" class="hidden">                            
        <p>
            <input id="rRacers" type="text" class="medium" />
            <span class="notification information">Racers</span>
        </p>

        <p>
            <input id="rVideoSet" type="text" class="medium" />
            <span class="notification information">Video set</span>
        </p>                                                         
     </div>                
</form>

<a href="#" id="saveConfiguration" class="graybuttonBig">Save everything!</a> 

JavaScript:

JavaScript:

$(document).ready(function(){
    $("#saveConfiguration").click(function(){
        alert( $("form").serialize() );   
    });
});  

All I'm getting is an empty string.

我得到的只是一个空字符串。

回答by Felix Kling

You have to give your form elements names!

你必须给你的表单元素name

This is independent of jQuery. Every form element must have a nameto be considered for form submission as successful control:

这独立于 jQuery。每个表单元素都必须有一个name被视为表单提交成功的控件

A successful control is "valid" for submission. Every successful control has its control namepaired with its current valueas part of the submitted form data set. A successful control must be defined within a FORMelement and must have a control name.

一个成功的控制是“有效的”提交。每个成功的控件都将其控件名称与其当前值配对,作为提交的表单数据集的一部分。一个成功的控件必须在一个FORM元素中定义并且必须有一个控件名称

jQuery just ignores those elements that don't have a name (or, depending on how it gets the elements, it might not even see them as the form itself has no reference to them).

jQuery 只是忽略那些没有名称的元素(或者,根据它获取元素的方式,它甚至可能看不到它们,因为表单本身没有对它们的引用)。

回答by Liam

Something else that prevents serializeArray()from serializing properly is disabledinputs. serializeArraydoes not serialise disabled inputs, in much the same way that disabled inputs are not submitted with the form.

其他阻止serializeArray()正确序列化的东西是disabled输入。serializeArray不会序列化禁用的输入,这与不随表单提交禁用的输入非常相似。

A successful control is "valid" for submission.

  • Controls that are disabledcannot be successful.

一个成功的控制是“有效的”提交。

  • 禁用的控件无法成功。

Source

来源

回答by whoknows

Please add a nameto your input field:

name在您的输入字段中添加一个:

<input type='text' name='give_some_name' />

In this fiddleI have added a nameand it is working fine.

这个小提琴中,我添加了一个name并且它工作正常。

回答by ssi-anik

I think the problem is that you're trying to select form like

我认为问题在于你试图选择像

$("form");

But this is equivalent to

但这相当于

getElementsByTagName("form");

This returns an array of objects.
So, instead you can use the #idselector, or use the index to access the form. Hope this helps.

这将返回一个对象数组。
因此,您可以改为使用#id选择器,或使用索引来访问表单。希望这可以帮助。

回答by pranav shinde

First of all, you need to give name attribute to all input controls like textboxes etc. Then please check whether your id's are clashing or not, I had the same id for form and the one section that's where my problem was.

首先,您需要为所有输入控件(如文本框等)提供 name 属性。然后请检查您的 ID 是否冲突,我的表单 ID 和我的问题所在的部分相同。

回答by user12229470

You have not referenced the form correctly in your javascript

您没有在 javascript 中正确引用表单

Try changing to this:

尝试更改为:

$('#gamesForm').serialize();

回答by Ryan S. Cole

And your reference is wrong. Try:

而且你的参考是错误的。尝试:

$("#gamesForm").serialize();