Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Где ошибка? нужна помощь (https://javascript.ru/forum/misc/6747-gde-oshibka-nuzhna-pomoshh.html)

Papa 19.12.2009 02:00

Где ошибка? нужна помощь
 
Есть класс, но почемуто не видит this... ??
Ошибка: this.check is not a function
function CheckField(f) {
	this.noCheck = new Array();
	this.inputs = f.getElementsByTagName('input');
	this.textareas = f.getElementsByTagName('textarea');
	function isCheck(elem) {
		if(elem.indexOf('check') != -1) return true;
		return false;
	}
	this.check = function() {
		if(noCheck.length > 0) {
			notice(noCheck, '#b6b6b6');
			noCheck.length = 0;
		}		
		if(inputs.length > 0) {
			for(var i = 0; i < inputs.length; i++) {
				if(inputs[i].getAttribute('type') == 'text' && isCheck(inputs[i].id)) {					
					if(inputs[i].value == '') noCheck.push(inputs[i].id);				
				}
			}			
		}
		if(textareas.length > 0) {
			for(var i = 0; i < textareas.length; i++) {
				if(isCheck(textareas[i].id)) {					
					if(textareas[i].value == '') noCheck.push(textareas[i].id);				
				}
			}
		}
		if(noCheck.length == 0) return true;
		else {
			notice(noCheck, 'red');
			return false;
		}
	}
	this.notice = function(f, c) {	
		for(var i in f) {
			document.getElementById(f[i].substr(0, f[i].indexOf('_'))+'_caption').style.color = c;
			document.getElementById(f[i]).style.border = '1px solid '+c;
		}		
	}	
	f.onsubmit = function() {return this.check();}
}
var cf = new CheckField(document.forms[0]);

PeaceCoder 19.12.2009 03:16

и не будет видеть. в контесте CheckField this = новый обьект, а не на f. А в контексте onsubmit, this=f

http://javascript.ru/tutorial/object
подробно по твоей части http://javascript.ru/tutorial/object/thiskeyword

function CheckField(f) {
    f.noCheck = new Array();
    f.inputs = f.getElementsByTagName('input');
    f.textareas = f.getElementsByTagName('textarea');
    f.isCheck = function(elem) {
        if(elem.indexOf('check') != -1) return true;
        return false;
    }
    f.check = function() {

        if(noCheck.length > 0) {
            notice(noCheck, '#b6b6b6');
            noCheck.length = 0;
        }        
        if(inputs.length > 0) {
            for(var i = 0; i < inputs.length; i++) {
                if(inputs[i].getAttribute('type') == 'text' && isCheck(inputs[i].id)) {                    
                    if(inputs[i].value == '') noCheck.push(inputs[i].id);                
                }
            }            
        }
        if(textareas.length > 0) {
            for(var i = 0; i < textareas.length; i++) {
                if(isCheck(textareas[i].id)) {                    
                    if(textareas[i].value == '') noCheck.push(textareas[i].id);                
                }
            }
        }
        if(noCheck.length == 0) return true;
        else {
            notice(noCheck, 'red');
            return false;
        }
    }
    this.notice = function(f, c) {    
        for(var i in f) {
            document.getElementById(f[i].substr(0, f[i].indexOf('_'))+'_caption').style.color = c;
            document.getElementById(f[i]).style.border = '1px solid '+c;
        }        
    }    
    f.onsubmit = function() {return this.check();}
}
var cf = new CheckField(document.forms[0]);


правда не помню сработает такое в контексте check() this=f, схавает это браузер?


Часовой пояс GMT +3, время: 07:39.