javascript方法在chrome中不起作用

javascript method not working in chrome

本文关键字:不起作用 chrome 方法 javascript      更新时间:2024-02-06

我在Chrome中遇到javascript问题。我有一个表单,其中有一个文本输入和一个复选框。文本输入的定义如下:

<input type="text" style="display:block;", id="BroadsofTypeInput",  onblur = "CustomValidation.validate.pText(this, 550,'Required field! Maximum 550 characters!');">  

在onblur事件上调用的Javascript内联函数验证输入。当输入为空时,它会在input标记旁边添加一个div元素。Div元素显示一条错误消息。

接下来,当我点击复选框时,这段代码被称为:

   function SetVisibleContent(control) {   
        var dropdown = document.getElementById("dropdonwtypes");
        var textfield = document.getElementById("BroadsofTypeInput");
        if (control.checked == true) {        
            ShowControl(dropdown, true);
            ShowControl(textfield, false);
            dropdown.value = "";           
        }
        else {           
            ShowControl(dropdown, false);
            ShowControl(textfield, true);           
            textfield.value = "";            
        }
        RemoveErrors();
        debugger;
    }

问题是:当我单击要选中的复选框时,错误消息应该删除,但事实并非如此。但这还不是全部,当开发工具在Chrome中打开并且调试器行被捕获时,它就可以工作了
这就是它变得疯狂的地方。当处于调试模式并且调试器行被捕获时,如果我单击F8,消息将被删除。如果我点击上面写着"恢复脚本执行(F8)"的蓝色按钮,它就不起作用。

下面是下载示例代码的链接:https://drive.google.com/file/d/0B0cu8N689t1MUVYxdkZhT2V4bzg/view?usp=sharing

var CustomValidation = CustomValidation || {};
CustomValidation.validate = {
    errorEmpty: "Required field!",
    errorFormat: "Wrong format!",
    errorLength: "Value to long!",
    errorEmptyValues: "value cannot be empty!", 
    errorInterval: "Value not in interval!",
    submitButton: null,
   
    //Error message methods
    errorMsgEmpty: function (control, errorMsg) {
        $(control).focus();
        $(control).css("background", "#FF9F9F");
        if (errorMsg != "" && errorMsg != undefined)
            $(control).after('<div class="errorInputMessage">' + errorMsg + '</div>');
        else
            $(control).after('<div class="errorInputMessage">' + this.errorEmpty + '</div>');       
    },
    errorMsgEmptyValues: function (control, errorMsg) {       
        if (errorMsg != "" && errorMsg != undefined)
            $(control).after('<div class="errorInputMessage">' + errorMsg + '</div>');
        else
            $(control).after('<div class="errorInputMessage">' + this.errorEmptyValues + '</div>');
    },
    errorMsgFormat: function (control, errorMsg) {
        $(control).focus();
        $(control).css("background", "#FF9F9F");
        if (errorMsg != "" && errorMsg != undefined)
            $(control).after('<div class="errorInputMessage">' + errorMsg + '</div>');
        else
            $(control).after('<div class="errorInputMessage">' + this.errorFormat + '</div>');
    },
    errorMsgLength: function (control, errorMsg) {     
        $(control).focus();
        $(control).css("background", "#FF9F9F");
        if (errorMsg != "" && errorMsg != undefined)
            $(control).after('<div class="errorInputMessage">' + errorMsg + '</div>');
        else
            $(control).after('<div class="errorInputMessage">' + this.errorLength + '</div>');
    },   
    errorMsgInterval: function(control, errorMsg)
    {
        $(control).focus();
        $(control).css("background", "#FF9F9F");
        if (errorMsg != "" && errorMsg != undefined)
            $(control).after('<div class="errorInputMessage">' + errorMsg + '</div>');
        else
            $(control).after('<div class="errorInputMessage">' + this.errorInterval + '</div>');
    },
    //Reset input background
    resetBack: function (control) {
        $(control).css("background", "#FFFFFF");
    },
    //Check length
    checkLength: function (control, length, errorMsg) {
        if (control.value != undefined) {
            if (length != 0 && control.value.length > length) {
                this.errorMsgLength(control, errorMsg);
                return false;
            }
        }
        return true;
    },
    //Disable submit button
    disableButton: function(){
        var inputArray = $(".errorInputMessage");
       
        if (inputArray.length > 0)
            this.submitButton.setAttribute("disabled", "disabled");
        else
            this.submitButton.removeAttribute("disabled");
    },
    //Validation methods	
    pInt: function (control, length, errorMsg) {
    
        var errorControl = $(control).next();
        if ($(errorControl).hasClass("errorInputMessage"))
            $(errorControl).remove();
        if (length != 0)
            if (!this.checkLength(control, length, errorMsg))
                return false;           
        if (control.value == "") {
            this.errorMsgEmpty(control, errorMsg);
        }
        //else if (isNaN(control.value) && !$(errorControl).hasClass("errorInputMessage")) {
        else if(isNaN(control.value)){
            this.errorMsgFormat(control, errorMsg);
        }
        else
            this.resetBack(control);
    },
    pFloat: function (control, length, errorMsg) {
        var errorControl = $(control).next();
        if ($(errorControl).hasClass("errorInputMessage"))
            $(errorControl).remove();
        if (length != 0)
            this.checkLength(control, length, errorMsg);
        if (control.value == "") {
            this.errorMsgEmpty(control, errorMsg);
        }
        //else if (isNaN(parseFloat(control.value)) && !$(errorControl).hasClass("errorInputMessage")) {
        else if (isNaN(control.value)) {
            this.errorMsgFormat(control, errorMsg);
        }
        else
            this.resetBack(control);
    },
    pRegex: function (control, length, regexValue, errorMsg) {
        var errorControl = $(control).next();
        if ($(errorControl).hasClass("errorInputMessage"))
            $(errorControl).remove();
        if (length != 0)
            this.checkLength(control, length, errorMsg);
        if (control.value == "") {
            this.errorMsgEmpty(control, errorMsg);
        }
        else if (regexValue.exec(control.value) == null) {
            this.errorMsgFormat(control, errorMsg);
        }
        else
            this.resetBack(control);
    },
    pText: function (control, length, errorMsg) {
     
        var errorControl = $(control).next();
        if ($(errorControl).hasClass("errorInputMessage"))
            $(errorControl).remove();
        if (length != 0) 
            this.checkLength(control, length, errorMsg);            
        
        var inputArrayError = $(".errorInputMessage");
        if (inputArrayError.length > 0)
            return false;
        if (control.value == "") {
            this.errorMsgEmpty(control, errorMsg);
        }
        else
            this.resetBack(control);
    },
    pIntervalInt:function (control, errorMsg, min, max)
    {
        var errorControl = $(control).next();
        if ($(errorControl).hasClass("errorInputMessage"))
            $(errorControl).remove();
       
        if (min > parseInt(control.value) || parseInt(control.value) > max) {        
            this.errorMsgInterval(control, errorMsg);
        }
        else
            this.resetBack(control);
    },    
    //Validate submit
    formSubmit: function () { 
        var error=0;
        var inputArray = $(".validation");
        var inputArrayError = $(".errorInputMessage");
        if (inputArrayError.length > 0)
            return false;
        else {
            for (var i = 0; i < inputArray.length; i++) {
                var errorControl = $(inputArray[i]).next();
                if ($(errorControl).hasClass("errorInputMessage"))
                    $(errorControl).remove();
                if (inputArray[i].value == "" || inputArray[i].value == null) {
                    this.errorMsgEmpty(inputArray[i], '');
                    error += 1;
                }                
            }
        }
        if (error > 0)
            return false;
        else 
            return true;
    },
    /* f - validate float,
     * i - validate int,
     * s - validate string,
     * l - validate interval
    */
    formSubmitControls: function (controls, errormsg, controlsChecked, filters, intervals) {
    
        if (this.formSubmit()) {
            for (var i = 0; i < controls.length; i++) {
                //Remove error message so it cannot be repeated           
                var errorControl = $(controls[i]).next();
                if ($(errorControl).hasClass("errorInputMessage"))
                    $(errorControl).remove();
                if (controlsChecked[i] != "" && controlsChecked[i] != undefined) {
                    if (controlsChecked[i][0].checked) {
                        this.filterControl(controls[i], 0, filters[i], errormsg[i], intervals[i])
                    }
                    else
                        this.resetBack(controls[i]);
                }
                else {
                    if (controls[i].value == "") {
                        //this.errorMsgEmptyValues(controls[i], errormsg[i]);
                        this.filterControl(controls[i], 0, filters[i], errormsg[i], intervals[i])
                        return false;
                    }
                    else {
                        this.filterControl(controls[i], 0, filters[i], errormsg[i], intervals[i])
                    }
                }
            }
            var inputArray = $(".errorInputMessage");
            if (inputArray.length > 0)
                return false;
        }
        else
            return false;
        return true;
    },
    filterControl: function (control, length, filters, error, interval) {
       
        var s = "";
        var min, max;        
        if (interval != "" && interval != undefined) {
            var s = interval.split(";");
            min = s[0];
            max = s[1];
        }
        if (filters.indexOf("f") != -1) {
            this.pFloat(control, length, error)
        }
        if (filters.indexOf("i") != -1) {
            this.pInt(control,length, error)
        }
        if (filters.indexOf("s") != -1) {
            this.pText(control, length, error)
        }
        if (filters.indexOf("l") != -1) {
            this.pIntervalInt(control, error, min, max)
        }
    }
   
}
 function SetVisibleContent(control) {       
        var dropdown = document.getElementById("dropdonwtypes");
        var textfield = document.getElementById("BroadsofTypeInput");
        if (control.checked == true) {            
            ShowControl(dropdown, true);
            ShowControl(textfield, false);
            dropdown.value = "";           
        }
        else {           
            ShowControl(dropdown, false);
            ShowControl(textfield, true); 
			
            textfield.value = "";            
        }
        RemoveErrors();
		debugger;
    }
    function RemoveErrors() {
        var inputArrayError = $(".errorInputMessage");
      
        for (var i = 0; i < inputArrayError.length; i++) {   
            $(inputArrayError[i]).remove();            
        }
    }
	 function ShowControl(control, show)
    {
        if (show) {
            //control.setAttribute("style", "display:block;visibility:visible;width:450px;");
            control.setAttribute("style", "display:block;width:450px;");
        }
        else {
            //control.setAttribute("style", "display:none;visibility:hidden;width:450px;");
            control.setAttribute("style", "display:none;width:450px;");
        }
    }
.info, .success, .warning, .errorInputMessage {
	border: 1px solid;
	margin: 10px 0px;
	padding:5px 5px 5px 5px;
	background-repeat: no-repeat;
	background-position: 5px center;
	}
	.info {
	color: #00529B;
	background-color: #BDE5F8;
	background-image: url('info.png');
	}
	.success {
	color: #4F8A10;
	background-color: #DFF2BF;
	background-image:url('success.png');
	}
	.warning {
	color: #9F6000;
	background-color: #FEEFB3;
	background-image: url('warning.png');
	}
	.errorInputMessage {
	color: #D8000C;
	background-color: #FFBABA;
	}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<form>
	<label>Text Bro!</label>
	
 <input type="text" style="display:block;", id="BroadsofTypeInput",  onblur = "CustomValidation.validate.pText(this, 550,'Required field! Maximum 550 characters!');"> 
 
<input type="text" style="display:none;", id="dropdonwtypes",  onblur = "CustomValidation.validate.pText(this, 550,'Required field! Maximum 550 characters!');">        
<input id="IsBroadsoft" name="IsBroadsoft" onchange="SetVisibleContent(this);" type="checkbox" value="true"> Is Broadsoft
 <br />
<input type="submit" value="Save" onclick="CustomValidation.validate.formSubmit()">
</form>

错误已删除!但随后文本输入上的模糊事件再次触发,因此错误再次出现。在调试模式下单击F8时的不同行为,我想是因为您没有单击文档主体,所以模糊事件没有触发。这一行,你会看到:

pText: function (control, length, errorMsg) {
  console.log('......ptext.......');
  // other codes....................
}