0

我正在尝试使用 jquery 序列化我的表单,但它没有按预期工作。许多其他字段中只有一个字段被序列化。

我的 HTML:

<form action="/Devis/auto" id="theForm" method="post" novalidate="novalidate"
class="ui-formwizard ui-helper-reset ui-widget ui-widget-content ui-corner-all">
    <div id="fieldWrapper">
        <fieldset id="first" class="step ui-formwizard-content" style="display: none; margin-left: -1148px; width: 1148px;">
            <legend>Information conducteur</legend>
            <div class="editor-label">
                <label for="DriverInfoViewModel_DriverInfo_NoClaimsDegree">Degré B/M</label>
            </div>
            <div class="editor-field">
                <select data-val="true" data-val-range="[fr-FR: Range]" data-val-range-max="22"
                data-val-range-min="-3" data-val-required="Le champ Degré B/M est obligatoire."
                id="DriverInfoViewModel_DriverInfo_NoClaimsDegree" name="DriverInfoViewModel.DriverInfo.NoClaimsDegree"
                class="ui-wizard-content valid" disabled="disabled">
                    <option value="-3">-3</option>
                    <option value="-2">-2</option>
                    <option value="-1">-1</option>
                    <option value="0">0</option>
                    <option selected="selected" value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                    <option value="11">11</option>
                    <option value="12">12</option>
                    <option value="13">13</option>
                    <option value="14">14</option>
                    <option value="15">15</option>
                    <option value="16">16</option>
                    <option value="17">17</option>
                    <option value="18">18</option>
                </select> <span class="text-error field-validation-valid" data-valmsg-for="DriverInfoViewModel.DriverInfo.NoClaimsDegree"
                data-valmsg-replace="true"></span>

            </div>
            <div>
                <label for="DriverInfoViewModel_DriverInfo_DrivingLicenceDate">Date de permis de conduire</label>
            </div>
            <div>
                <input class="datefield ui-wizard-content valid" data-val="true" data-val-required="Le champ Date de permis de conduire est obligatoire."
                id="DriverInfoViewModel_DriverInfo_DrivingLicenceDate" name="DriverInfoViewModel.DriverInfo.DrivingLicenceDate"
                type="date" value="10/02/2013"> <span class="text-error inline field-validation-valid" data-valmsg-for="DriverInfoViewModel.DriverInfo.DrivingLicenceDate"
                data-valmsg-replace="true"></span>

            </div>
            <div class="editor-label">
                <label for="DriverInfoViewModel_DriverInfo_DisasterHistory">Nombre de sinistres</label>
            </div>
            <div class="editor-field">
                <select data-val="true" data-val-required="Le champ Nombre de sinistres est obligatoire."
                id="DriverInfoViewModel_DriverInfo_DisasterHistory" name="DriverInfoViewModel.DriverInfo.DisasterHistory"
                class="ui-wizard-content valid" disabled="disabled">
                    <option selected="selected" value="NoDisaster">Pas de sinistre</option>
                    <option value="NoDisasterAndLessThan3YearsDMInsured">Pas de sinistre et garantie DM de moins de 3 ans</option>
                    <option value="OneDisaster">1 sinistre</option>
                    <option value="TwoDisasters">2 sinistres</option>
                    <option value="MoreThanTwoDisasters">3 sinistres</option>
                </select> <span class="text-error field-validation-valid" data-valmsg-for="DriverInfoViewModel.DriverInfo.DisasterHistory"
                data-valmsg-replace="true"></span>

            </div>
        </fieldset>
        <fieldset id="vehicleinfo" class="step ui-formwizard-content" style="display: none; width: 1148px; margin-left: -1148px;">
            <legend>Information véhicule</legend>
            <div class="editor-label">
                <label for="CarInfoViewModel_CarInfo_CarValue">Valeur du véhicule</label>
            </div>
            <div class="editor-field">
                <input data-val="true" data-val-required="Le champ Valeur du véhicule est obligatoire."
                id="CarInfoViewModel_CarInfo_CarValue" name="CarInfoViewModel.CarInfo.CarValue"
                type="text" value="15000" class="ui-wizard-content valid" disabled="disabled"> <span class="text-error field-validation-valid" data-valmsg-for="CarInfoViewModel.CarInfo.CarValue"
                data-valmsg-replace="true"></span>

            </div>
            <div class="editor-label">
                <label for="CarInfoViewModel_CarInfo_AudioValue">Valeur de l'équipement audio</label>
            </div>
            <div class="editor-field">
                <input data-val="true" data-val-required="Le champ Valeur de l'équipement audio est obligatoire."
                id="CarInfoViewModel_CarInfo_AudioValue" name="CarInfoViewModel.CarInfo.AudioValue"
                type="text" value="1000" class="ui-wizard-content valid" disabled="disabled"> <span class="text-error field-validation-valid" data-valmsg-for="CarInfoViewModel.CarInfo.AudioValue"
                data-valmsg-replace="true"></span>

            </div>
            <div class="editor-label">
                <label for="CarInfoViewModel_CarInfo_EngineCapacity">Cylindrée</label>
            </div>
            <div class="editor-field">
                <input data-val="true" data-val-required="Le champ Cylindrée est obligatoire."
                id="CarInfoViewModel_CarInfo_EngineCapacity" name="CarInfoViewModel.CarInfo.EngineCapacity"
                type="text" value="1500" class="ui-wizard-content valid" disabled="disabled"> <span class="text-error field-validation-valid" data-valmsg-for="CarInfoViewModel.CarInfo.EngineCapacity"
                data-valmsg-replace="true"></span>

            </div>
            <div class="editor-label">
                <label for="CarInfoViewModel_CarInfo_AdditionalSeats">Nombre de places additionnelles</label>
            </div>
            <div class="editor-field">
                <input data-val="true" data-val-required="Le champ Nombre de places additionnelles est obligatoire."
                id="CarInfoViewModel_CarInfo_AdditionalSeats" name="CarInfoViewModel.CarInfo.AdditionalSeats"
                type="text" value="1" class="ui-wizard-content valid" disabled="disabled"> <span class="text-error field-validation-valid" data-valmsg-for="CarInfoViewModel.CarInfo.AdditionalSeats"
                data-valmsg-replace="true"></span>

            </div>
            <div class="editor-label">
                <label for="CarInfoViewModel_CarInfo_FirstRegistration">Date de mise en circulation</label>
            </div>
            <div class="editor-field">
                <input class="datefield ui-wizard-content valid" data-val="true" data-val-required="Le champ Date de mise en circulation est obligatoire."
                id="CarInfoViewModel_CarInfo_FirstRegistration" name="CarInfoViewModel.CarInfo.FirstRegistration"
                type="date" value="01/01/2013" disabled="disabled"> <span class="text-error field-validation-valid" data-valmsg-for="CarInfoViewModel.CarInfo.FirstRegistration"
                data-valmsg-replace="true"></span>

            </div>
        </fieldset>
        <fieldset id="comparatif" class="step ui-formwizard-content" style="display: block; width: 1148px; margin-left: 0px;">
            <div id="result"></div>
        </fieldset>
        <fieldset id="customize" class="step ui-formwizard-content" style="display: none; width: 1148px; margin-left: -1148px;">
            <legend>Customize</legend>
            <div id="custom"><span>137,21</span>

            </div>
        </fieldset>
    </div>
</form> <pre></pre>

我的脚本:

$('pre').text(JSON.stringify($('#theForm').serialize(), null, '  '));

请参阅jsfiddle上的代码

我试过做:

$('#theForm').serializeArray()

知道我做错了什么吗?

4

5 回答 5

5

尝试删除

disabled="disabled"

在序列化之前。

于 2013-03-08T12:30:51.180 回答
3

从 jquery API 文档,http ://api.jquery.com/serializeArray/ :

.serializeArray() 方法使用成功控制的标准 W3C 规则来确定它应该包含哪些元素;特别是该元素不能被禁用

尝试从您的和标签中删除disabled="disabled"属性。inputselect

于 2013-03-08T12:34:48.050 回答
2

这是来自http://api.jquery.com/serialize/的摘录:

注意:只有“成功的控件”被序列化为字符串。由于未使用按钮提交表单,因此没有序列化提交按钮值。对于要包含在序列化字符串中的表单元素的值,该元素必须具有 name 属性。来自复选框和单选按钮(“单选”或“复选框”类型的输入)的值仅在它们被选中时才被包括在内。来自文件选择元素的数据未序列化。

也许它会有所帮助。

于 2013-03-08T12:26:07.613 回答
2

除了一个之外的所有表单元素都被禁用(属性disabled="disabled"),这导致它们不会被序列化。

于 2013-03-08T12:30:12.710 回答
0

试试这个

$('#theForm').stringify()
于 2013-03-08T12:21:25.463 回答