簡體   English   中英

如果選中復選框,則需要刪除

[英]if checkbox is checked remove required

用戶必須 select 至少一個復選框,一旦選中一個復選框,我想從所有復選框中刪除required ,然后如果用戶取消選擇,則不會選中任何復選框, required他們的聲譽。
我的代碼不起作用。 我在這里找到了很多類似的答案,但沒有一個對我有用,所以我再次問這個問題。

這是jsFiddle

 var checkbox_required = $('input[type="checkbox"]'); checkbox_required.prop('required', true); if (checkbox_required.is(':checked')) { checkbox_required.prop('required', false); } else { checkbox_required.prop('required', true); }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="奢侈品與配飾">奢侈品與配飾 </label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="女士時尚">女士時尚 </label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="男士時尚">男士時尚 </label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="美妝與護膚">美妝與護膚 </label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="兒童">兒童 </label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="內衣">內衣 </label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="家居">家居 </label> <input type="submit" value="submit"> </form>

用戶必須至少選擇一個復選框,一旦選中一個復選框,我想從所有復選框中刪除required ,然后,如果用戶取消選擇,則沒有選中任何復選框,並認為它們是required
我的代碼無法正常工作。 我在這里找到了許多類似的答案,但沒有一個對我有用,所以我再次提出這個問題。

這是jsFiddle

 var checkbox_required = $('input[type="checkbox"]'); checkbox_required.prop('required', true); if (checkbox_required.is(':checked')) { checkbox_required.prop('required', false); } else { checkbox_required.prop('required', true); }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="奢侈品與配飾">奢侈品與配飾</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="女士時尚">女士時尚</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="男士時尚">男士時尚</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="美妝與護膚">美妝與護膚</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="兒童">兒童</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="內衣">內衣</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="家居">家居</label> <input type="submit" value="submit"> </form>

用戶必須至少選擇一個復選框,一旦選中一個復選框,我想從所有復選框中刪除required ,然后,如果用戶取消選擇,則沒有選中任何復選框,並認為它們是required
我的代碼無法正常工作。 我在這里找到了許多類似的答案,但沒有一個對我有用,所以我再次提出這個問題。

這是jsFiddle

 var checkbox_required = $('input[type="checkbox"]'); checkbox_required.prop('required', true); if (checkbox_required.is(':checked')) { checkbox_required.prop('required', false); } else { checkbox_required.prop('required', true); }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="奢侈品與配飾">奢侈品與配飾</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="女士時尚">女士時尚</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="男士時尚">男士時尚</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="美妝與護膚">美妝與護膚</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="兒童">兒童</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="內衣">內衣</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="家居">家居</label> <input type="submit" value="submit"> </form>

用戶必須至少選擇一個復選框,一旦選中一個復選框,我想從所有復選框中刪除required ,然后,如果用戶取消選擇,則沒有選中任何復選框,並認為它們是required
我的代碼無法正常工作。 我在這里找到了許多類似的答案,但沒有一個對我有用,所以我再次提出這個問題。

這是jsFiddle

 var checkbox_required = $('input[type="checkbox"]'); checkbox_required.prop('required', true); if (checkbox_required.is(':checked')) { checkbox_required.prop('required', false); } else { checkbox_required.prop('required', true); }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="奢侈品與配飾">奢侈品與配飾</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="女士時尚">女士時尚</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="男士時尚">男士時尚</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="美妝與護膚">美妝與護膚</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="兒童">兒童</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="內衣">內衣</label> <label class="produit_type"> <input type="checkbox" name="produit_type[]" value="家居">家居</label> <input type="submit" value="submit"> </form>

如果您有多個具有不同名稱的組,則此解決方案完美工作注意:添加取消選中驗證

  $(document).on('change', 'input[type=checkbox]',function (e) {
      var inputname = $(this).attr('name');
      $("input[name='"+inputname+"']").prop('required', false);  
  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM