仅当选择了特定的单选按钮时,jQuery才会验证文本

时间:2018-01-19 20:18:10

标签: jquery validation radio rules

我需要仅在选择某些单选按钮时才需要字段文本: 在这种情况下,只有选择“Resp.Inscripto”,“Iva Exento”或“Monotributo”值(其中任何一个)。如果有人选择“Consumidor Final”,则不需要输入文本。

我正在使用此代码,但它不起作用。

我做错了什么?

    <script type="text/javascript">

    $(function(){

        $('#form1').validate({

  rules: {
    contrato_cuit: { 
     required: '#contrato_cuittipo[value="Resp. Inscripto"]:checked'
    },
    contrato_cuit: { 
     required: '#contrato_cuittipo[value="IVA Exento"]:checked'
    },
    contrato_cuit: { 
     required: '#contrato_cuittipo[value="Monotributo"]:checked'
    }
  }         

        });
    })
</script>

<form name="form1" id="form1" method="post" action="xxxx.php" encType="multipart/form-data" >

CUIT
<input type="text" value='' name="contrato_cuit" id="contrato_cuit">

Resp. Inscripto
<input name="contrato_cuittipo" id="contrato_cuittipo_r" type="radio" value="Resp. Inscripto" required checked="checked" />

Monotributo
<input name="contrato_cuittipo" id="contrato_cuittipo_m" type="radio" value="Monotributo" required /> 

IVA Exento
<input name="contrato_cuittipo" id="contrato_cuittipo_i" type="radio" value="IVA Exento" required /> 

Consumidor Final
<input name="contrato_cuittipo" id="contrato_cuittipo_c" type="radio" value="Consumidor Final" required />

<input type="submit" name="button" id="btnsubmit" value=" ENVIAR "/> 

</form>

谢谢, 旧金山

1 个答案:

答案 0 :(得分:0)

您可以利用jQuery .validate()不会尝试验证已禁用的输入这一事实。

因此,点击radio,检查它是否为值Consumidor Final

如果是,则只需禁用文本输入。如果没有,请启用它。

&#13;
&#13;
$(function(){

  $("#form1").validate();
  
  $("#form1 input[type='radio']").on("click",function(){
    if($(this).val()=="Consumidor Final"){
      console.log("Text input disabled");
      $("#contrato_cuit").prop("disabled",true);
    }else{
      console.log("Text input enabled");
      $("#contrato_cuit").prop("disabled",false);
    }
  });
  
});
&#13;
.error{
  font-size:1.4em;
  color:red;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet"/>


<form name="form1" id="form1" method="post" action="xxxx.php" encType="multipart/form-data" >

  CUIT
  <input type="text" value='' name="contrato_cuit" id="contrato_cuit" required>

  Resp. Inscripto
  <input name="contrato_cuittipo" id="contrato_cuittipo_r" type="radio" value="Resp. Inscripto" required checked="checked" />

  Monotributo
  <input name="contrato_cuittipo" id="contrato_cuittipo_m" type="radio" value="Monotributo" required /> 

  IVA Exento
  <input name="contrato_cuittipo" id="contrato_cuittipo_i" type="radio" value="IVA Exento" required /> 

  Consumidor Final
  <input name="contrato_cuittipo" id="contrato_cuittipo_c" type="radio" value="Consumidor Final" required />

  <input type="submit" name="button" id="btnsubmit" value=" ENVIAR "/> 

</form>
&#13;
&#13;
&#13;