简体   繁体   English

重新计算点击次数的功能

[英]function to recount on click

I am running a validation function onclick. 我正在运行验证功能onclick。 When you click on the button initially, it goes through each field and makes sure the validations are good or not. 最初单击该按钮时,它会遍历每个字段,并确保验证是否正确。 It currently only tells me via console how many errors there are, or if there were no errors. 它当前仅通过控制台告诉我有多少错误,或者是否没有错误。

It works the first time! 它是第一次工作! But when I fix the error and click the button again, it still says that there is an error from before it was fixed. 但是,当我修复错误并再次单击按钮时,它仍然表示存在来自修复前的错误。

My question is, is there a way to have the button recount or "refresh"? 我的问题是,有没有办法让按钮重新计数或“刷新”? Here is the code that I have 这是我的代码

function moveOn(){
    if (errors > 0) {
        console.log(errors);
    } else if(errors === 0){
        console.log("no errors!");
    }
};

here is the rest of the code 这是其余的代码

var errors = 0;
function validate(){


$(".validName").each(function() {
    var val = $(this).val();
    var minLength = defaultMinNameLength
    //check data-min-length if exists then 
    // min length = thatValue
    if (validateName(val) && validateLength(val,minLength)){
        inlineValidate(this, { '.error': 'hide', '.correct': 'fadeIn', '.incorrect': 'hide' },"#ccc");
    } else {
        errors++;
        inlineValidate(this, { '.error': 'fadeIn', '.correct': 'hide', '.incorrect': 'fadeIn' }, "#ff9999");
    }
});

$(".validLetNum").each(function() {
    var val = $(this).val();
    var minLength = defaultMinNameLength
    if (validateLetterNum(val) && validateLength(val,minLength)){
        inlineValidate(this, { '.error': 'hide', '.correct': 'fadeIn', '.incorrect': 'hide' },"#ccc");
    } else {
        errors++;
        inlineValidate(this, { '.error': 'fadeIn', '.correct': 'hide', '.incorrect': 'fadeIn' }, "#ff9999");
    }
});

$(".validEmail").each(function() {
    var val = $(this).val();
    var minLength = defaultMinNameLength
    if (validateEmail(val) && validateLength(val,minLength)){
        inlineValidate(this, { '.error': 'hide', '.correct': 'fadeIn', '.incorrect': 'hide' },"#ccc");
    } else {
        errors++;
        inlineValidate(this, { '.error': 'fadeIn', '.correct': 'hide', '.incorrect': 'fadeIn' }, "#ff9999");
    }
});
// console.log(errors);
};

function moveOn(){
    if (errors > 0) {
        console.log(errors);
    } else if(errors === 0){
        console.log("no errors!");
    }
};
function moveOn(){
    if (errors > 0) {
        // Correct errors
        errors = 0;
    } else if(errors === 0){
        console.log("no errors!");
    }
};

You can possibly fix the issue by changing: 您可以通过更改可能解决这个问题:

var errors = 0;
function validate(){

To: 至:

var errors;
function validate() {
    errors = 0;

NOTE : From your code it's not very clear how you call moveOn() (or even whey you call validate() ) so I cannot confidently confirm that the above should fix your issue. 注意 :从您的代码中,您不清楚如何调用moveOn() (甚至不知道您是否调用validate() ),因此我无法自信地确认以上内容可以解决您的问题。

CAUTION 警告

  • Try not to clutter the global scope. 尽量不要使全局范围混乱。 You want to use localized variables. 您要使用局部变量。 If you need to use a variable like errors in another function, pass that variable to the second function, or that may be an indication that your two functions should be combined into one. 如果您需要在另一个函数中使用类似errors的变量,请将该变量传递给第二个函数,或者这可能表明您应该将两个函数合并为一个。
  • Separate your JS code from your HTML. 将JS代码与HTML分开。 Rather than use an element's onclick attribute, it is better to leverage the power of jQuery and bind a click event to the element instead. 与其使用元素的onclick属性,不如利用jQuery的强大功能,并将click事件绑定到该元素,效果更好。

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

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