简体   繁体   English

选择单选按钮后如何使必填项为空

[英]How to make required be empty after selecting radio button

I am trying to change the name of required when it is not selected and I did it. 当未选择required的名称时,我正在尝试更改它的名称。 When I click "Send" without selecting it first, it will show an error, but when I selected it again but not the first radio button where I put the required syntax it still shows the same error that forced me to select the first radio button. 当我单击“发送”而不先选择它时,它将显示一个错误,但是当我再次选择它但没有在第一个单选按钮中放置required语法时,它仍然显示相同的错误,迫使我选择第一个单选按钮。 Is there a way to make it right, please? 请问有办法解决吗? Thank you for the help. 感谢您的帮助。

This is my code: 这是我的代码:

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <form method="post" action="" id="form"> <div class="container"> <div class="intro-text left-0 text-left text-black p-4 rounded " style="background-color: #ffbe76"> <div class="section-heading-upper"> <p class="mb-3 lead">1. ......? </p> </div> <div class="row ml-2"> <div class="col-auto"> <div class="form-group mx-auto option"> <h2 class="section-heading mb-2"> <span class="text-center section-heading-upper">Layanan</span> </h2> <div class="form-check"> <input class="form-check-input" type="radio" name="penting1" id="1penting1" value="100.00" required="" oninvalid="this.setCustomValidity('Pick one')" oninput="this.setCustomValidity('')"> <label class="form-check-label" for="exampleRadios1"> Sangat Penting </label> </div> <div class="form-check"> <input class="form-check-input" type="radio" name="penting1" id="1penting2" value="81.25"> <label class="form-check-label" for="exampleRadios2"> Penting </label> </div> <div class="form-check"> <input class="form-check-input" type="radio" name="penting1" id="1penting3" value="62.50"> <label class="form-check-label" for="exampleRadios3"> Tidak Penting </label> </div> <div class="form-check"> <input class="form-check-input" type="radio" name="penting1" id="1penting4" value="43.75"> <label class="form-check-label" for="exampleRadios4"> Sangat Tidak Penting </label> </div> </div> </div> <div class="col-auto"> <div class="form-group mx-auto option"> <h2 class="section-heading mb-2"> <span class="text-center section-heading-upper">Kepuasan Layanan</span> </h2> <div class="form-check"> <input class="form-check-input" type="radio" name="puas1" id="1puas1" value="100.00" required oninvalid="this.setCustomValidity('Pick one')" oninput="setCustomValidity('')"> <label class="form-check-label" for="exampleRadios1"> Sangat Puas </label> </div> <div class="form-check"> <input class="form-check-input" type="radio" name="puas1" id="1puas2" value="81.25"> <label class="form-check-label" for="exampleRadios2"> Puas </label> </div> <div class="form-check"> <input class="form-check-input" type="radio" name="puas1" id="1puas3" value="62.50"> <label class="form-check-label" for="exampleRadios3"> Tidak Puas </label> </div> <div class="form-check"> <input class="form-check-input" type="radio" name="puas1" id="1puas4" value="43.75"> <label class="form-check-label" for="exampleRadios4"> Sangat Tidak Puas </label> </div> </div> </div> </div> <div class="text-center mt-2"> <input class="btn btn-primary btn-lg" value="Send" id="singlebutton" name="submit" type="submit" onclick="check()"> </div> </form> 

You can remove required attributes and test whether a specific one is checked using jQuery as follows: 您可以删除必需的属性并使用jQuery测试是否检查了特定属性,如下所示:

if ($('input[name=penting1]:checked').length > 0) {
    //selected
}

if ($('input[name=puas1]:checked').length > 0) {
    //selected
}

it will work :) 它将起作用:)

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

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