简体   繁体   English

如何根据表单中的单选按钮选择按需填写字段

[英]How can I a field as required based on selection of radio button in a form

There is a custom radio button.有一个自定义单选按钮。 On select of 'yes' of that button another field is there which should not be required在选择该按钮的“是”时,另一个字段不应该是必需的

$("#btnFormSubmit").click(function() {
  var radioValue = $("input[name='CustomDuration']:checked").val();
  if (radioValue == 'Yes') {
    $("#frmCertificateRequirement").validate({
      rules: {
        ValidDurationMonths: {
          required: false
        }
      }
    })
  }

Firstly you need to call validate() when the page loads, not when the submit button of the form is clicked.首先,您需要在页面加载时调用validate() ,而不是在单击表单的提交按钮时调用。

With regards to your issue, the required property will accept a selector you can use to dynamically set the validation requirements for the element.关于您的问题, required属性将接受一个选择器,您可以使用它来动态设置元素的验证要求。 Try this:尝试这个:

$("#frmCertificateRequirement").validate({
  rules: {
    ValidDurationMonths: {
      required: 'input[name="CustomDuration"]:checked'
    }
  }
});

If you do not want to show the Required Message when the radio button is checked .如果您不想在选中单选按钮时显示所需消息。 Then There is no closing of your code , please correct it.那么您的代码没有关闭,请更正。

HTML Code HTML代码

<form name="btnFormSubmit" id="btnFormSubmit" method="post">
    <div>
        <label for="username">Username/Email</label>
        <input type="text" name="username" autocomplete="false" readonly onfocus="this.removeAttribute('readonly');">
    </div>
    <button type="submit" id="btnFormSubmit">submit</button>
</form>
<input type="checkbox" name="CustomDuration" id="CustomDuration" value="Yes">checkbox

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>

JQUERY Code with perfect closing具有完美关闭的 JQUERY 代码

$("#btnFormSubmit").click(function () {
   var radioValue = $('input[name="CustomDuration"]:checked').val();

   if (radioValue == "Yes") {
      $("#login_form").validate({
         rules:{
            username: 
            {
                required: false,
            }
         },
      });
    }
 }); /*You have missing this closing*/

暂无
暂无

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

相关问题 如何根据单选按钮的选择使表单元素的行为有所不同 - How can I make a form element behave differently based on radio button selection 显示基于单选按钮选择的表单字段 - Show form fields based on radio button selection 根据单选按钮选择填写表单字段 - Fill form fields based on radio button selection 如何根据单选按钮选择更改文本输入的最大长度验证? - How can I change the maxlength validation for a text input based on a radio button selection? 当我单击另一个单选按钮时,如何清除单选按钮选择中的内容? - How can I clear content from a radio button selection when I click on another radio button? 如何选择选项选项用户选中单选按钮是和删除所需如果选中否在jQuery或Javascript提交表单之前 - How to required selection option user checked radio button Yes and removed required if checked on No In jQuery or Javascript before submitting form 如何根据以前的单选选项禁用单选按钮 - How to Disable radio button based on previous radio selection 当表单中的特定输入字段不为空时如何更改单选按钮选择 - How to change a radio button selection when a specfic input field in the form is not empty 根据单选按钮显示内容(但仅在提交表单后显示) - Display Content Based on Radio Button Selection (but only once form is submitted) 根据单选按钮选择更改多个表单字段 - Change multiple form fields based on radio button selection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM