简体   繁体   English

选择后如何删除必填属性

[英]How to remove required attribute after select

I did an asset delivery system so there are two options for the user to select which is NOT YET DELIVERED and ALREADY DELIVERED. 我做了一个资产交付系统,所以有两个选项供用户选择“尚未交付”和“已交付”。

I used javascript to allow date input to display if the user selects ALREADY DELIVERED. 如果用户选择“已交付”,我使用javascript允许显示日期输入。 I added required for the calendar so that the user cannot submit the details if user doesn't choose a date and it worked perfectly. 我为日历添加了required ,以便在用户未选择日期且工作正常的情况下用户无法提交详细信息。

However,when the user selects NOT YET DELIVERED, the system doesn't display the calendar and therefore cannot submit the form due to the calendar being required . 但是,当用户选择“尚未交付”时,系统不会显示日历,因此由于required日历,因此无法提交表单。

I wanted the calendar to be required if the user selects ALREADY DELIVERED but not for NOT YET DELIVERED. 我希望如果用户选择“已交付”而不是“尚未交付”,则required日历。

Thank you in advance. 先感谢您。

 $(function() { with name = 'status') $(document).on("change", "input[name='status']", function() { console.log("Testing"); var checkedValue = $(this).val(); console.log(checkedValue); else; checkedValue == 1 ? $(".box").show() : $(".box").hide(); }) }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span class="alignright"> <div> <br /> <label> <input type="radio" name="status" value="0" required /> Not yet delivered </label> <br /> <label> <input type="radio" name="status" value="1" required /> Already delivered </label> </div> </span> <span class="alignleft"> <div class="box">DELIVERY DATE<input type="date" name="deliverydate" id="deliverydate" required="" /> </div> </span> 

You can do it this way: 您可以这样操作:

$(function() { 
  with name='status')
  $(document).on("change", "input[name='status']", function() {
    console.log("Testing");
    var checkedValue = $(this).val();
    console.log(checkedValue);

    else;

    if(checkedValue == 1) {
       $(".box").show();
       $('input#deliverydate').prop('required',true);
    } else {
       $(".box").hide();
       $('input#deliverydate').prop('required',false);
    }
  })
});

Hope it helps. 希望能帮助到你。

Instead of setting the required value on the date input, set it depending on which option the user selects. 不用在日期输入上设置所需的值,而是根据用户选择的选项进行设置。 You can do this at the same time as hiding/showing it. 您可以在隐藏/显示的同时执行此操作。

 $(document).on("change", "input[name='status']", function() { var checkedValue = $(this).val(); if(checkedValue == 1){ $(".box").show(); $(".box").prop("required",true); } else { $(".box").hide(); $(".box").prop("required",false); } console.log($(".box").prop("required")); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span class="alignright"> <div> <br /> <label> <input type="radio" name="status" value="0" required /> Not yet delivered </label> <br /> <label> <input type="radio" name="status" value="1" required /> Already delivered </label> </div> </span> <span class="alignleft"> <div class="box">DELIVERY DATE<input type="date" name="deliverydate" id="deliverydate" /> </div> </span> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM