2017-04-05 66 views
1

我有一個由下拉列表選擇項目生成的JavaScript動態表格。 我的表格包含複選框,顯示名稱的列和輸入數量的列。 我想要的是讓所有文本框禁用untel其行被檢查。 這是我的JavaScript代碼和殘疾人不適合我。JavaScript:點擊複選框啓用文本框

<script> 
    $(function() { 
     $("#ProduitID").change(function() { 

      $.get("/Demandes/GetGabarit", { produit: $("#ProduitID").val() }, function (data) { 

       $("#Gabarit").empty(); 
       $.each(data, function (index, row) { 

        $("#Gabarit").append($("<tr>").append("<td>" + "<input type=checkbox name= gabarit class=Check value='" + row.CodeBarre + "'/>" + "</td>" 
                  + "<td>" + row.Designation + "</td>" 
                 + "<td>" + "<input type=text style=width:50px; name=Qt class=Quantite/>" + "</td>" 

        )); 


       }); 
      }) 
     }); 

    }); 
    $(document).ready(function() { 
     $('#checkBoxAll').click(function() { 
      if ($(this).is(":checked")){ 
       $('.Check').prop('checked', true); 
       $('.Quantite').prop('disabled', false); 
} 
      else{ 
       $('.Check').prop('checked', false); 
       $('.Quantite').prop('disabled', true); 
} 
     }); 
    }); 

</script> 

查看:

<div class="form-group"> 
      @Html.LabelFor(model => model.Produit, "Produit", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 

       @Html.DropDownListFor(p => p.CodeBarre, ViewBag.Produit as SelectList, "Sélectionner un produit", new { id = "ProduitID", @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.Produit, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.CodeBarre, "Gabarit", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 

       <table class="table table-striped"> 
        <thead> 
         <tr> 
          <th width="25%"><input type="checkbox" id="checkBoxAll" /></th> 
          <th width="25%">@Html.DisplayNameFor(model => model.Designation)</th> 

          <th width="25%">@Html.DisplayNameFor(model => model.Quantite)</th> 

         </tr> 
        </thead> 
        <tbody id="Gabarit"> 
         <tr> 
          <td class="Designation"></td> 

          <td class="Quantite" ></td> 
          <td class="Check"></td> 
         </tr> 
        </tbody> 
       </table> 
       @Html.ValidationMessageFor(model => model.CodeBarre, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Submit" class="btn btn-default" /> 
      </div> 
     </div> 

謝謝。

+0

請你還張貼與這個JS一起的HTML?發佈一個JSFiddle,它不僅僅是一段代碼,這樣我們可以解決你有的小問題,而不是爲你重新創建整個代碼。這樣,你會得到更多的回答你的問題。 – ProgrammerV5

+0

@ ProgrammerV5我編輯了我的帖子,謝謝 – oumaima

+0

謝謝,但這不是呈現的HTML,它只是一個視圖。我發佈了一個解決方案,可以幫助您在自己的代碼中找到問題,希望它有所幫助! – ProgrammerV5

回答

0

試試這個代碼 -

$(document).on('change', '#checkBoxAll', function() { 

      if ($(this).is(":checked")){ 
       $('.Check').prop('checked', true); 
       $('.Quantite').prop('disabled', false); 
} 
      else{ 
       $('.Check').prop('checked', false); 
       $('.Quantite').prop('disabled', true); 
} 

    }); 
+0

謝謝您的建議,但它不適用於我:( – oumaima

+0

是發生任何錯誤... –

+0

沒有錯誤,但所有的文本框啓用之前檢查 – oumaima

0

不知道你最後的HTML(您要發送一個視圖),我可以幫你這個代碼:

 <input type="checkbox" id="checkBoxAll" value="second_checkbox" checked="checked"> 


     <input type="textbox" id="txt1" class="Quantite" value="1"> 
     <input type="textbox" id="txt2" class="Quantite" value="2"> 
     <input type="textbox" id="txt3" class="Quantite" value="3"> 



$(document).ready(function() { 
    $('#checkBoxAll').change(function() { 
     if ($(this).is(":checked")){ 
      $('.Quantite').prop('disabled', 'disabled'); 
        } 
     else{ 
      $('.Quantite').prop('disabled',''); 
        } 
    }); 
}); 

工作JSFiddle

+0

謝謝,但它不起作用:( – oumaima

+0

如果你點擊複選框,文本框被禁用(嘗試輸入一些文本,你會發現這是不可能的)。 – ProgrammerV5

+0

我希望在沒有複選框被選中之前,所有的文本框都被禁用後,文本框會被啓用 – oumaima