[英]The order of my jquery validator is working wrong, and I have no idea why
My form validator works, but it works in a certain order, if I check the checkbox, it works fine, but if I fill the inputs first and then use the checkbox, it not works, unless I type something in the inputs 我的表单验证器可以工作,但是可以按一定顺序工作,如果我选中复选框,则可以正常工作,但是如果我先填写输入,然后使用复选框,则它不起作用,除非我在输入中输入内容
$(document).ready(function() { $('#send').attr('disabled', true); $('.input,#check').on('keyup', function() { var text_value = $('.input-cpk').val(); if (text_value !== '' && (document.getElementById('check').checked)) { $('#send').attr('disabled', false); } else { $('#send').attr('disabled', true); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="block oh fr col-9 margem-d2 text-left" action="enviar-calculo.php" method="post" target="votar" name="cpk"> <input type="text" name="nome" id="nome" class="input-cpk col-20"> <input type="number" name="telefone" class="input-cpk-tel"> <input type="submit" value="CALCULAR" id="send" class="f-josefin-sb f-branca bg-amarelo botao" onclick="output();"> <input type="checkbox" id="check" name="others" /> </form>
You only run your validation code during the keyup
event, which only happens on the input boxes. 你只有在运行验证代码
keyup
事件,该事件仅发生在输入框。 You also need to do validation during a click
event on the checkbox. 您还需要在复选框上的
click
事件期间进行验证。
You can put multiple event names in the argument to .on()
, to handle both with the same code. 您可以在
.on()
的参数中放入多个事件名称,以使用同一代码处理这两个事件。
You also have an incorrect class .input
, there are no elements with class="input"
in the HTML. 您也有一个错误的类
.input
,HTML中没有class="input"
元素。 I've changed it to .input-cpk
. 我将其更改为
.input-cpk
。
$(document).ready(function() { $('#send').prop('disabled', true); $('.input-cpk,#check').on('keyup click', function() { var text_value = $('.input-cpk').val(); if (text_value !== '' && (document.getElementById('check').checked)) { $('#send').prop('disabled', false); } else { $('#send').prop('disabled', true); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="block oh fr col-9 margem-d2 text-left" action="enviar-calculo.php" method="post" target="votar" name="cpk"> <input type="text" name="nome" id="nome" class="input-cpk col-20"> <input type="number" name="telefone" class="input-cpk-tel"> <input type="submit" value="CALCULAR" id="send" class="f-josefin-sb f-branca bg-amarelo botao" onclick="output();"> <input type="checkbox" id="check" name="others" /> </form>
It's because you're using the wrong selector. 这是因为您使用了错误的选择器。
Try this $('input,#check').on('keyup', function() { 试试这个$('input,#check')。on('keyup',function(){
Instead of this $('.input,#check').on('keyup', function() { 代替这个$('。input,#check')。on('keyup',function(){
Additionally you might want to use the .on('change') instead of .on('keyup') 另外,您可能希望使用.on('change')而不是.on('keyup')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.