序列化表单在jQuery中不起作用(Serialize form not working in jQuery)

58 IT屋

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

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:

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

All I'm getting is an empty string.

解决方案

You have to give your form elements names!

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

A successful control is "valid" for submission. Every successful control has its control name paired with its current value as part of the submitted form data set. A successful control must be defined within a FORM element and must have a control name.

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).

能否请您看看并帮助我意识到我在哪里出错?这是jsfiddle链接: http://jsfiddle.net/Hitman666/QcEkj/1/ 但这也是代码



HTML:



 < form action =# id = gamesForm> 
< p>
< input id = gName type = text class = medium />
< span class = notification information>游戏名称< / span>
< / p>

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

< br />
<!-额外类型的附加数据->
< 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>视频集< / span>
< / p>
< / div>
< / form>

< a href =# id = saveConfiguration class = graybuttonBig>保存所有内容!< / a>


JavaScript:



 < code> $(document).ready(function(){
$(#saveConfiguration)。click(function(){
alert($( form)。serialize() );
});
});


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


解决方案

您必须给表单元素名称 s!



< p>这独立于jQuery。每个表单元素都必须具有名称,以便以 成功控制




成功控件对提交有效。每个成功的控件都有其控件名称和其< a href = http://www.w3.org/TR/html401/interact/forms.html#current-value rel = noreferrer>当前值作为提交的表单数据集。必须在 FORM中定义成功的控件 元素,并且必须具有控件名称




jQuery仅忽略那些没有名称的元素(或取决于其名称)获取元素,它可能甚至看不到它们,因为表单本身没有对它们的引用。


本文地址:IT屋 » 序列化表单在jQuery中不起作用