这个JavaScript函数以前用过,直到我把它变得更复杂,因为我试图将更多错误检查推送到前端。
这是前端代码,只是为了简洁而重要的部分:
<body>
<form id="form1" runat="server">
<div>
<script type = "text/javascript" >
function checkThings()
{
var jobSeekerFirstName = document.getElementsByName('txtJobSeekerFirstName')[0].value
var jobSeekerLastName = document.getElementsByName('txtJobSeekerLastName')[0].value
var jobSeekerUserName = document.getElementsByName('txtJobSeekerUserName')[0].value
var jobSeekerPassword = document.getElementsByName('txtJobSeekerPassword')[0].value
var jobSeekerPhoneNumber = document.getElementsByName('txtJobSeekerPhoneNumber')[0].value
var jobSeekerEmail = document.getElementsByName('txtJobSeekerEmailAddress')[0].value
var jobSeekerAnswer = document.getElementsByName('txtJobSeekersSecurityAnswer')[0].value
if ((jobSeekerFirstName != '') && (jobSeekerLastName != '') && (jobSeekerUserName != '') &&
(jobSeekerPassword != '') && (jobSeekerPhoneNumber != '') && (jobSeekerEmail != '') &&
(jobSeekerAnswer != '')) // First check all fields entered
{
if (jobSeekerUserName.length == 6) {
var emAddrCounter;
var emlength = jobSeekerEmail.length;
var emailCorrect = false;
for (emAddrCounter = 0; ((emAddrCounter < emlength) && (emailCorrect == false)) ; emAddrCounter++) {
var emChar = jobSeekerEmail.charAt(emAddrCounter);
if (emChar == '@') emailCorrect = true;
}
if (emailCorrect == false) {
var emailNoAtChar = " Email address must contain @ character. Please reenter";
document.getElementsById('txtMessageBox').Text = emailNoAtChar;
return false;
}
else {
return true;
}
}
else
{
var userNameNotSix = " User name must be exactly six characters long. Please reenter";
document.getElementsByName('txtMessageBox').Text = userNameNotSix;
return false;
}
}
else
{
var allElementsPresent = " All Fields must be filled in first. Please reenter all fields";
document.getElementsById('txtMessageBox').Text = allElementsPresent;
return false;
}
}
</script>
<asp:ScriptManager ID="scripman1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>
致电行
<asp:Button ID="btnNewJobSeekerRegistration" runat="server"
Text=" Register me as a new Job Seeker "
OnClientClick= "return checkThings();"
OnClick="btnNewJobSeekerRegistration_Click" />
我一直在
操作已完成
它似乎跳过了所有JavaScript函数和其中的所有错误检查。有什么想法吗?
答案 0 :(得分:-3)
当你的JavaScript代码被&#34;没有被调用时#34;通常是因为它有问题。当JavaScript的任何行引发异常,并记住这个JavaScript代码没有编译但在运行时立即解释和执行时,我猜测它下面的每一段代码都被简单地跳过,后端的代码隐藏自动执行。要调试它,您需要在浏览器的调试器中打开开发人员的控制台。对我来说这是Chrome,所以请转到:自定义和控制Google Chrome /更多工具/开发人员工具(或 Ctrl + Shift + i ) ,然后转到控制台。现在从Visual Studio中运行您的代码,并观察JavaScript代码中引发的异常或错误。对我来说,这个错误是&#34; JobSeekerRegistration.aspx:100 Uncaught TypeError:document.getElementsById不是函数&#34;。我所做的只是在我的代码的两行中将document.getElementsById更改为document.getElementsByName并且瞧它工作了。特别感谢Aristos,但我的答案是最好的。