检查功能不起作用,含义不变 [英] Check function is not working variable meaning unchanged
问题描述
你好,我在jQuery女巫中有一个寄存器记录,它仅验证一个字段,除了最后一部分创建了一个变量检查并将其含义设置为0以外,其他所有东西都在工作.在检查一切正常的情况下,它将更改为1,如果不是0,最后如果!=为0,则显示下一个按钮div.即使我正确填写了这些字段,它也不会显示,这可能意味着含义保持不变,因为如果我在声明了我的位置将变量的含义设置为1,则会出现该按钮.这是我的脚本代码:
Hello I have a register scrip in jQuery witch only validates a field, everything is working except the last part where I create a variable check and set its meaning to 0. On the checks if everything is okay it will change to 1, if not to 0 and at the end if != to 0 show the next button div. Even if I fill in those fields correctly it doesn't show, and that probably means that the meaning stays unchanged, because if I set a variable meaning to 1 where i was declared , the button appears. Here is code of my script :
<script type="text/javascript">
//password validation ==================================
var check = 0;
$(document).ready(function() {
$('input[name*="password"]').keyup(validatepass);
});
function validatepass(){
var password = $('input[name="password"]').val();
var repassword = $('input[name="repassword"]').val();
var isValid = /^[a-zA-Z0-9]*$/.test(password);
var length = password.length;
if (isValid && (length > 4) && (length < 16)){
if(password == repassword){
$('input[name="password"]').attr('class', 'valid');
$('input[name="repassword"]').attr('class', 'valid');
check = 1;
} else {
$('input[name="repassword"]').attr('class', 'invalid');
check = 0;
}
if(password == ""){
$('input[name="password"]').attr('class', 'invalid');
$('input[name="repassword"]').attr('class', 'invalid');
check = 0;
} else {
$('input[name="password"]').attr('class', 'valid');
check = 1;
}
} else {
$('input[name="password"]').attr('class', 'invalid');
$('input[name="repassword"]').attr('class', 'invalid');
check = 0;
}
}
//username validation ========================
$(function () {
jQuery('#username').on('input', function () {
$("#username").removeClass();
var username = $(this).val();
var isValid = /^[a-zA-Z0-9]*$/.test(username);
var length = username.length;
if (isValid && (length > 4) && (length < 16)){
$("#username").addClass("valid");
check = 1;
} else {
$("#username").addClass("invalid");
check = 0;
}
});
});
//Email validation-------------------------------
$(function () {
jQuery('#email').on('input', function () {
$("#email").removeClass();
var username = $(this).val();
var isValid = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(username);
if (isValid){
$("#email").addClass("valid");
check = 1;
} else {
$("#email").addClass("invalid");
check = 0;
}
});
});
/// button show if everything is ok
$(document).ready(function() {
if(check != 0){
$("#loginsubmitbutton2").show();
}
});
</script>
推荐答案
我不得不重做一些代码,在这里查看整个小提琴:
I had to rework some of your code, have a look at the whole fiddle here:
我做的主要事情:
在每个输入事件中,检查表单中存在的<input>
字段的数量是否与类别为valid
的<input>
字段的数量相同.因为在这种情况下,您所有的字段都经过了验证,因此您可以显示按钮.
On every input event, check if the amount of <input>
fields that exist in your form is the same as the amount of <input>
fields with the class valid
. Because if that's the case, all your fields are validated and you can show the button.
这意味着不再需要check
变量.
This means there is no more need for a check
variable.
您主要做错的是此代码:
What you mainly did wrong was this code:
$(document).ready(function() {
if(check != 0){
$("#loginsubmitbutton2").show();
}
});
它检查check
变量是否不是0 AS SOON AS (页面加载).您可以在脚本顶部将check
设置为0,这意味着当代码的这一部分运行时,它会始终为0,因此该按钮将永远不会显示.
It checks if the check
variable is not 0 AS SOON AS the page loads. You set your check
to 0 at the top of your script, meaning it will ALWAYS be 0 when this part of your code runs, so the button is never shown.
这篇关于检查功能不起作用,含义不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!