2017-08-25 142 views
0

我的複選框總是返回false。有人可以幫助我。這是我的html div。Checkbox.Ischecked總是給出錯誤

<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="[email protected](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="[email protected](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> 
      } 
     } 
+0

但我得到正確的數據爲其他屬性,如customerGuid和contactInfo。只有複選框是錯誤的 – user2597617

+0

然後,您可以向我們展示實際標記,因爲您向我們展示的內容不匹配任何這些選擇器。 – Taplar

+0

你從哪裏得到變量'i'?此外,如果這是一個循環,你是否打算覆蓋數據對象? –

回答

0

您的輸入元素未正確關閉。關閉輸入標籤。 add /到您的輸入元素的末尾