2014-04-18 123 views
-1

我想在某些输入内的值为空时另一个元素为空时向元素添加一个类。Javascript函数IF声明不起作用

由于某种原因,它只是登陆else语句,我以为我使用正确的语法来发送多个参数,但也许我错了。

任何想法?

var btnElementRegistro = document.getElementById('btnGuardar'); 
    btnElementRegistro.addEventListener('click' , validar); 

function validar(){ 

var elementNombre = document.getElementById('txtNombre'), 
    elementApellido1 = document.getElementById('txtApellido1'), 
    elementCel = document.getElementById('numCelular'); 


    addClass(txtNombre,txtApellido1,numCelular); 


} 

function addClass(pName, pApellido, pCel){ 

if(pName.value === '' && pApellido === '' && pCel ===''){ 

    pName.className = "error"; 
    pApellido.className = "error"; 
    pCel.className = "error"; 

}else{ 
    pName.className = "noError"; 
    pApellido.className = "noError"; 
    pCel.className = "noError"; 


    } 

} 

在此先感谢。

编辑:我想要使用if/else,以便我可以调用另一个函数来显示一个div(PopUp)时添加.noError。

var elementoVerInfo = document.getElementById('btnGuardar'), 
elementoBotonCerrar = document.getElementById('btnCerrar'); 

elementoVerInfo.addEventListener('click', function() { 
displayPopUp('popUpCorrecto2'); 
}); 

elementoBotonCerrar.addEventListener('click', function() { 
hidePopUp('popUpCorrecto2'); 
}); 

function displayPopUp(pIdDivToShow){ 
var fElementDivToShow = document.getElementById(pIdDivToShow), 
newClass =''; 
newClass = fElementDivToShow.className.replace('hide',''); 
fElementDivToShow.className = newClass + ' show'; 
} 

function hidePopUp(pIdDivToShow){ 
var fElementDivToShow = document.getElementById(pIdDivToShow), 
newClass =''; 
newClass = fElementDivToShow.className.replace('show',''); 
fElementDivToShow.className = newClass + ' hide'; 
} 
+2

你应该传递变量名字 - 'addClass(elementNombre,elementApellido1,elementCel);'等等,而不是ID的的这些变量('txtNombre','txtApellido1'和'numCelular')。 – lifetimes

+0

您只检查其中一个变量的值... if(pName.value ===''&& pApellido ===''&& pCel ==='') –

+0

当您调试应用程序时,是参数定义?我猜测其中一个或全部都是未定义的,并且使你的平等陈述是错误的。 – VtoCorleone

回答

1

我想你想是这样的:

function validar(){ 

    var elementNombre = document.getElementById('txtNombre'), 
     elementApellido1 = document.getElementById('txtApellido1'), 
     elementCel = document.getElementById('numCelular'); 

    /*call it using the variables not the string identifiers.*/ 
    addClass(elementNombre, elementApellido1, elementCel); 
    /*addClass(txtNombre,txtApellido1,numCelular);*/ /* wrong */   
} 

function addClass(pName, pApellido, pCel){ 
    pName.className = pName.value === '' ? 'error' : 'noError'; 
    pApellido.className = pApellido.value === '' ? 'error' : 'noError'; 
    pCel.className = pCel.value === '' ? 'error' : 'noError'; 
} 

编辑回答在评论问题

一旦noError为,我可以在哪里添加对PopUp函数的调用补充?

如果你想添加一个函数调用,那么你必须使用if,因为?只允许单个语句。或者你可以如下做到这一点:

function addClass(pName, pApellido, pCel){ 
    pName.className = pName.value === '' ? 'error' : 'noError'; 
    pApellido.className = pApellido.value === '' ? 'error' : 'noError'; 
    pCel.className = pCel.value === '' ? 'error' : 'noError'; 

    if (pName.className === 'noError' && 
     pApellido.className === 'noError' && 
     pCel.className === 'noError'){ 
     callFunction(); /* call here the function you want */ 
    } 
} 

以上是一般的方法,因为我完全不知道,如果你想打电话给每个输入或只有一次的功能。如果你的目的是要求每个输入的功能,那么你可以做这样的:

function addClass(pName, pApellido, pCel){ 
    if (pName.value === ''){ 
     pName.className = 'error'; 
    } 
    else{ 
     pName.className = 'noError'; 
     displayPopUp(pName.id); 
    } 

    if (pApellido.value === ''){ 
     pApellido.className = 'error'; 
    } 
    else{ 
     pApellido.className = 'noError'; 
     displayPopUp(pApellido.id); 
    } 

    if (pCel.value === ''){ 
     pCel.className = 'error'; 
    } 
    else{ 
     pCel.className = 'noError'; 
     displayPopUp(pCel.id); 
    } 

} 
+0

工作就像一个魅力,谢谢!进一步的参考是什么?呢? –

+0

我正在使用if/else,因为一旦添加了类noError,我想使用添加到编辑中的代码打开PopUp。在删除代码时,一旦添加noError,我可以在哪里添加对PopUp函数的调用? –

+0

@CodeGrasshopper这是'if-then-else'的快捷方式。 '?'之前的部分是条件,在'?'后面是'后面'分支,后面是':''else'分支。 – agarwaen

1

这是问题:

var elementNombre = document.getElementById('txtNombre'), 
elementApellido1 = document.getElementById('txtApellido1'), 
elementCel = document.getElementById('numCelular'); 

addClass(txtNombre,txtApellido1,numCelular); 

txtNombretxtApellido1numCelular是元素的ID,而不是变量名。

你应该通过你定义的变量名:

addClass(elementNombre, elementApellido1, elementCel); 
+0

谢谢!我看到我在那里犯了一个错误,但它不起作用。它仍然添加noError类。 –

+1

@CodeGrasshopper你确定输入元素是完全空的(你有没有尝试console.logging他们)?你能否给我一个快速的jsFiddle来表明这个问题? – lifetimes

+0

我设法使用agarwean的解决方案修补它。认为它让我感到困惑,为什么你的代码看起来不应该工作。如果你想要,我仍然可以从这一个小提琴。 –