Checkbox.Ischecked总是给出错误

时间:2017-08-25 21:44:03

标签: jquery html checkbox

我的复选框始终返回false。有人可以帮助我。这是我的html div。

<div class="large-12 columns">
    <label for="Data_AlternatePhones[i]_IsPrimary">
        <input class="toggle_enabled hidden-field" id="Data_AlternatePhones[i]_IsPrimary" name="Data.AlternatePhones[i].IsPrimary" value="true" type="checkbox">
        <span class="custom checkbox"></span>
        <input name="Data.AlternatePhones[i].IsPrimary" value="false" type="hidden">IsPrimary
    </label>
</div>

选中该复选框后,页面检查器代码会显示以下html代码段

<div class="large-12 columns">
    <label for="Data_AlternatePhones[i]_IsPrimary">
        <input class="toggle_enabled hidden-field" id="Data_AlternatePhones[i]_IsPrimary" name="Data.AlternatePhones[i].IsPrimary" value="true" type="checkbox">
        <span class="custom checkbox checked"></span>
        <input name="Data.AlternatePhones[i].IsPrimary" value="false" type="hidden">IsPrimary
    </label>
</div>

如果未选中该复选框,则页面检查器代码会显示以下html代码段

<div class="large-12 columns">
    <label for="Data_AlternatePhones[i]_IsPrimary">
        <input class="toggle_enabled hidden-field" id="Data_AlternatePhones[i]_IsPrimary" name="Data.AlternatePhones[i].IsPrimary" value="true" type="checkbox">
        <span class="custom checkbox"></span>
        <input name="Data.AlternatePhones[i].IsPrimary" value="false" type="hidden">IsPrimary
   </label>
</div>

jQuery代码中的我的数据变量在两种情况下都给出了isPrimary = false。

var data =
    {
        customerGuid: $("#Data_CustomerGuid").val(),
        contactInfoType: type,
        contactInfo: $("#Data_AlternatePhones_"+i+"__PhoneNumber").val(),
                isPrimary: $("#Data_AlternatePhones_" + i + "__IsPrimary").is(':checked'), 
    isPrimaryTest: $("#Data_AlternatePhones[" + i + "]_IsPrimary > span").is(':checked'),                  
        contactInfoID: $("#Data_AlternatePhones_"+i+"__ID").val()
    };

Image of the data i get in the developer tool

请帮我解决这个问题。 谢谢。

这是我在cshtml文件中的实际标记。

     @if (Model.Data.AlternatePhones.Count > 0)
        {
            for (int i = 0; i < Model.Data.AlternatePhones.Take(2).Count(); i++)
            {
                <div style="float:right">
                    <a data-reveal-id="divFirstSetEditAlternatePhoneNumber@(i)" class="fa fa-pencil-square-o" data-i="@i" data-contactinfotype="phone" ></a>
                    <a class="fa fa-trash-o deletecontactinfo" data-customer-id="@Model.Data.CustomerGuid" data-info="@Model.Data.AlternatePhones[i].PhoneNumber" data-contactinfotype="phone"></a>
                </div>
                <div>
                    @Model.Data.AlternatePhones[i].PhoneNumber                        
                    @if (Model.Data.AlternatePhones[i].IsVerified)
                    {
                        <div class="fa fa-user"><span class="verified">Verified Customer</span></div><br />
                    }
                </div>
                <br />
                <div id="divFirstSetEditAlternatePhoneNumber@(i)" class="reveal-modal tiny" data-reveal aria-labelledby="modalTitle" aria-hidden="true" role="dialog">
                    <div class="form custom med-6 columns popupModal">
                        <h2>
                            Edit Phone Number
                        </h2>
                        <form>
                            @Html.HiddenFor(x => Model.Data.CustomerGuid)
                            @Html.HiddenFor(x => Model.Data.AlternatePhones[i].ID)
                            <div class="row">
                                <div class="large-12 columns">
                                    @Html.WrappedTextBoxFor(x => Model.Data.AlternatePhones[i].PhoneNumber, htmlAttributes: new { @class = "toggle_enabled" })
                                </div>
                            </div>
                            <div class="row">
                                <div class="large-12 columns">
                                    @*@Html.WrappedCheckBoxFor(x => Model.Data.AlternatePhones[i].IsPrimary, htmlAttributes: new { @class = "toggle_enabled" })*@

                                    <label for="Data_AlternatePhones[i]_IsPrimary">
                                        <input class="toggle_enabled hidden-field" id="Data_AlternatePhones[i]_IsPrimary" name="Data.AlternatePhones[i].IsPrimary" value="true" type="checkbox">
                                        <span class="custom checkbox"></span>
                                        <input name="Data.AlternatePhones[i].IsPrimary" value="false" type="hidden">IsPrimary
                                    </label>

                                </div>
                            </div>
                            <div class="row">
                                <div class="large-12 columns">
                                    <input type="button" class="btnEditAlternatePhoneNumber button toggle_enabled" value="Save" />
                                    <button class="btnCloseEditAlternatePhoneNumber button toggle_enabled closediv">Cancel</button>
                                </div>
                                <a class="close-reveal-modal toggle_enabled">&#215;</a>
                            </div>


                        </form>
                    </div>
                </div>
            }
        }

1 个答案:

答案 0 :(得分:0)

您的输入元素未正确关闭。关闭输入标签。 将 / 添加到输入元素的结尾