使用window.onload在javascript onsubmit中加载多个函数 [英] load multiple function in javascript onsubmit using window.onload
问题描述
HY!我正在使用javascript来为每个使用单独的函数验证不同的表单,现在的问题是,当我在window.onload上加载一个函数时,它将正常工作,但是当我加载多个函数时,只有第一个将工作,其余的将不?要知道什么是错误,我使用try catch,这个错误将显示
Hy! i am using javascript to validate different forms using separate function for each, now the problem is that when i load one function on window.onload the it will work fine but when i load multiple function then only the first one will work and the rest will not? to know what is the error i use try catch and this error will shown
typeerror:cannot set property 'onsubmit' of null
这是我的代码...
this is my code...
//validation of registration page..
function validate_form_registration()
{
document.getElementById('form_reg').onsubmit=function()
{
if (document.getElementById('first_name').value.length=="")
{
document.getElementById('error').innerHTML="please enter first name";
document.getElementById('first_name').focus();
return false;
}
else if (document.getElementById('last_name').value.length=="")
{
document.getElementById('error').innerHTML="please enter Last name";
document.getElementById('last_name').focus();
return false;
}
else if (document.getElementById('username').value.length=="")
{
document.getElementById('error').innerHTML="please enter Username";
document.getElementById('username').focus();
return false;
}
else if (document.getElementById('mail').value.length=="")
{
document.getElementById('error').innerHTML="please enter Email";
document.getElementById('mail').focus();
return false;
}
else if (document.getElementById('dept').value.length=="")
{
document.getElementById('error').innerHTML="please enter your Department";
document.getElementById('dept').focus();
return false;
}
else if (document.getElementById('id_number').value.length=="")
{
document.getElementById('error').innerHTML="please enter ID number";
document.getElementById('id_number').focus();
return false;
}
else if (document.getElementById('phone').value.length=="")
{
document.getElementById('error').innerHTML="please enter Phone number";
document.getElementById('phone').focus();
return false;
}
else if (document.getElementById('address').value.length=="")
{
document.getElementById('error').innerHTML="please enter Your Adress";
document.getElementById('address').focus();
return false;
}
else
{
document.getElementById('error').innerHTML="";
return true;
}
};
};
//end of registration page...
//validation of login page.....
function validate_form_login()
{
document.getElementById('form_log').onsubmit=function()
{
if(document.getElementById('username').value.length=='')
{
document.getElementById('error').innerHTML='Please enter username';
document.getElementById('username').focus();
return false;
}
else if(document.getElementById('password').value.length=='')
{
document.getElementById('error').innerHTML='Please enter password';
document.getElementById('password').focus();
return false;
}
else
{
document.getElementById('error').innerHTML='';
return true;
}
};
};
//validation of application page..
function validate_form_aplic()
{
document.getElementById('form_anous').onsubmit=function(){
if(document.getElementById('select_item').value.length=='')
{
document.getElementById('error').innerHTML='Please select leave type';
document.getElementById('select_item').focus();
return false;
}
else if(document.getElementById('txtarea').value.length=='')
{
document.getElementById('error').innerHTML='Please enter application';
document.getElementById('txtarea').focus();
return false;
}
else if(document.getElementById('fromt').value.length=='')
{
document.getElementById('error').innerHTML='Please enter time';
document.getElementById('fromt').focus();
return false;
}
else if(document.getElementById('tot').value.length=='')
{
document.getElementById('error').innerHTML='Please enter time limit';
document.getElementById('tot').focus();
return false;
}
else
{
document.getElementById('error').innerHTML='';
return true;
}
};
};
window.onload=function()
{ try {
validate_form_aplic();
validate_form_login();
validate_form_registration();
}
catch (error)
{
alert(error);
}
};
如何解决?为什么window.onload可以覆盖所有其他的第一个函数,s?有没有办法解决它?
how to solve it? why the window.onload can overwrite the first function on all the other,s? is there a way to solve it?
推荐答案
你没有告诉我们onsubmit三次出现的错误,但显然你需要我们调试器...
getElementById中的一个或多个返回null。使用调试器查看哪个并检查HTML页面中的相应元素...
You do not tell us on which of the three occurrences of onsubmit you got the error, but obviously you have to us debugger...
One - or more - of the getElementById returns null. Use debugger to see which and check the corresponding element in your HTML page...
这篇关于使用window.onload在javascript onsubmit中加载多个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!