我只想验证此表单,但不断收到未捕获的类型错误:无法读取 null 的属性(读取"样式")



我尝试了所有的方法,从在表单上下移动脚本标记,使用refer,将js放在html文件中。。。等等。我只是不明白为什么这不起作用。plz help:(.通常我在使用DOM方法时不会遇到任何问题,但无论我做什么,它都会一直说元素为null。这就像它试图找到一些东西,但它找不到。

如果你真的修复了它,请提供一个很好的解释,这样我就可以在未来避免类似的错误,谢谢:-(

var traitementTimeOut;
function frm_valider(idFrm,url,idMsgDiv,InputIds){
if(js_validerFrm(InputIds)==false) return;
document.getElementById(idFrm).submit();   
functionAappeler = "frm_valider_traitement('"+url+"','"+idMsgDiv+"')";
traitementTimeOut=setTimeout(functionAappeler, 1000);  
}
function js_validerFrm(InputIds){
if(InputIds!=""){
var retourFunc=true;
var color_oblig='#A54241';var color_nonValide='#EF9C6A';var color_dtNonValide='yellow';
var msg_oblig               ="Champs obligatoiresnr";
var msg_nonValide           ="Contenu des champs n'est pas valide";
var msg_dtNonValide         ="Les champs mises en jaunes ne peuvent pas  tre des dates futuresnr";
var msg_dtNonValide_passe   ="Les champs mises en jaunes ne peuvent pas  tre des dates pass esnr";
var tableauStr              = InputIds.split('##');
var err_msg_dt="";var err_msg_olig="";var err_msg_nonValide ="";    
//------
for(j=0;j<tableauStr.length;j++){
tableauIds  = tableauStr[j].split(';;');
inputId     = tableauIds[0];
isOblig     = tableauIds[1];
regExp      = tableauIds[2];
if(document.getElementById(inputId).style.display==""){
if(document.getElementById(inputId)){

curFldValue = document.getElementById(inputId).value;
curFldValue=curFldValue.replace(/^s+/g,'').replace(/s+$/g,'');
if(isOblig==1){
if(curFldValue==""){
err_msg_olig=msg_oblig;
document.getElementById(inputId).style.backgroundColor=color_oblig;
retourFunc = false;
}
else if(regExp=="dateF"){                           
if(checkDateNonFutur(curFldValue)==true)    document.getElementById(inputId).style.backgroundColor='';
else{
err_msg_dt=msg_dtNonValide;
document.getElementById(inputId).style.backgroundColor=color_dtNonValide;
retourFunc = false;
}
}
else if(regExp=="dateP"){   
if(checkDateNonPasse(curFldValue)==true)    document.getElementById(inputId).style.backgroundColor='';
else{
err_msg_dt=msg_dtNonValide_passe;
document.getElementById(inputId).style.backgroundColor=color_dtNonValide;
retourFunc = false;
}
}
else if(!curFldValue.match(regExp)){
err_msg_nonValide=msg_nonValide;
document.getElementById(inputId).style.backgroundColor=color_nonValide;
retourFunc = false;         
}
else {document.getElementById(inputId).style.backgroundColor='';}
}//--if
else if(curFldValue!="") {
if(regExp=="dateF"){ 
if(checkDateNonFutur(curFldValue)==true)    document.getElementById(inputId).style.backgroundColor='';
else{
err_msg_dt=msg_dtNonValide;
document.getElementById(inputId).style.backgroundColor=color_dtNonValide;
retourFunc = false;
}
}
else if(regExp=="dateP"){
if(checkDateNonPasse(curFldValue)==true)    document.getElementById(inputId).style.backgroundColor='';
else{
err_msg_dt=msg_dtNonValide_passe;
document.getElementById(inputId).style.backgroundColor=color_dtNonValide;
retourFunc = false;
}
}
else if(!curFldValue.match(regExp)){
err_msg_nonValide=msg_nonValide;
document.getElementById(inputId).style.backgroundColor=color_nonValide;
retourFunc = false;             
}
else {document.getElementById(inputId).style.backgroundColor='';}
}
else {document.getElementById(inputId).style.backgroundColor='';}
}//if
}//if
else document.getElementById(inputId).style.backgroundColor='';
}
if(retourFunc==false) {alert(err_msg_dt+err_msg_olig+err_msg_nonValide);return false;}
return true;
}//if
else return true; 
}//function
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script defer src="./luck.js"></script>
</head>
<body>
<p align='center'><table width='100%' align='center'> 
<form method='POST' action="mngUtilisateur_liste_new_bean.php" name="ex_Utilisateur_new" id="ex_Utilisateur_new" 
target="ex_Utilisateur_new_iframe" >
<tr class='tr_1'><td width='30%'><b>Nom:</b></td> 
<td width='20%'><input id='nom_new' name='nom_new'
type='text' class='champTxt' size='60'></td></tr> 
<tr class='tr_1'><td width='30%'><b>Login</b></td> 
<td width='20%'><input id='login_new' name='login_new' 
type='text' class='champTxt' size='60'></td></tr>
<tr class='tr_1'><td width='30%'><b>Mot de passe</b> 
</td><td width='20%'><input id='pwd_new' name='pwd_new'  
type='password' class='champTxt' size='60'></td></tr>
<tr class='tr_1'><td width='30%'><b>confirmez le mot de passe:</b> 
</td><td width='20%'><input id='confpwd_new' name='confpwd_new'  
type='password' class='champTxt' size='60'></td></tr>
<tr class='tr_2'><td colspan='4' align='center'> 
<input type='button' value='Valider' onClick="if(document.getElementById('pwd_new').value == 
document.getElementById('confpwd_new').value)
frm_valider('ex_Utilisateur_new','mngUtilisateur_liste_new_bean.php','liste_newElem_msg','nom_new;;1;;^[a-zA-Z]+##login_new;;1;;^[a-zA-Z0-9]+##pwd_new;;1;;^[a-zA-Z0-9]+##employeur_new;;1;;^[0-9]+##group_new;;1;;^[0-9].*');
else {
document.getElementById('pwd_new').style.color='red';
document.getElementById('pwd_new').value = '';
document.getElementById('confpwd_new').style.color='red';
document.getElementById('confpwd_new').value = '';
} "></td></tr></form></table></p>
</body>
</html>

我看到了您的代码,我认为您有两个问题。

1-有2个变量未定义

tableauIds = tableauStr[j].split(";;");
inputId = tableauIds[0];

所以要解决它,你必须添加let(我更喜欢远离var关键字(

let tableauIds = tableauStr[j].split(";;");
let inputId = tableauIds[0];

2-你的HTML文件中的这行代码有问题

frm_valider('ex_Utilisateur_new','mngUtilisateur_liste_new_bean.php','liste_newElem_msg','nom_new;;1;;^[a-zA-Z]+##login_new;;1;;^[a-zA-Z0-9]+##pwd_new;;1;;^[a-zA-Z0-9]+##employeur_new;;1;;^[0-9]+##group_new;;1;;^[0-9].*');

(employeur_new(和(group_new(不作为输入的id存在,因此它给出了一个错误(无法读取null的读取"style"的属性(。

所以为了解决这个问题,我认为你想做以下选择之一

a-进行新的输入,一个输入id为(employeur_new(,另一个输入(group_new(

<tr class='tr_1'>
<td width='30%'>
<b>[some text here]</b> 
</td>
<td width='20%'>
<input id='employeur_new' name='[some name here]'  
type='[type]' class='champTxt' size='60'>
</td>
</tr>
<tr class='tr_1'>
<td width='30%'>
<b>[some text here]</b> 
</td>
<td width='20%'>
<input id='group_new' name='[some name here]'  
type='[type]' class='champTxt' size='60'>
</td>
</tr>

b-从代码中删除(employeur_new(和(group_new(。

我希望这就是你想要的。感谢

相关内容

最新更新