【问题标题】:functions are not being called or are not running函数没有被调用或没有运行
【发布时间】:2014-10-06 04:44:59
【问题描述】:

我已经为此工作了一个星期,但我终其一生都无法弄清楚我错过了什么,或者我是否在不应该的地方放了一些东西。在我开始对 confirmSubmit() 函数进行更改之前,代码运行良好,现在除了显示页面之外似乎没有任何工作正常。我也让老师看了,他说他也查不出来。我只是不知道了,也许你们中的任何一个都可以帮我解决这个问题。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="AS400.css" type="text/css" />
<title>Registration</title>

<script type="text/javascript">
    /* <![CDATA[ */
    // This JavaScript removes default values form field
       function doClear(theText) {
     if (theText.value == theText.defaultValue) {
         theText.value = ""
     }
 }

// This JavaScript allows characters except numbers
 function AcceptLetters(objtextbox)
{
var exp = /[^\D]/g;
objtextbox.value = objtextbox.value.replace(exp,'');
}

// This JavaScript allows numbers only
 function AcceptDigits(objtextbox)
{
var exp = /[^\d]/g;
objtextbox.value = objtextbox.value.replace(exp,'');
}

//This JavaScript confirms everything is filled out
function confirmSubmit()
{
var submitForm = window.confirm("Are you sure you want to submit the form?");
var Special offers = false;
var offers = false;



if (document.forms[0].name.value=="Name" || document.forms[0].name.value == "") {
     window.alert("Please input your name.");
    return false;}

 if (document.forms[0].email.value == "E-Mail Address" || document.forms[0].email.value == "") {
    window.alert("Please input your email.");
    return false;}

    if (document.forms[0].password1.value == "" || document.forms[0].password2.value == "") {
    window.alert("Please input your password twice.");
    retrun false;}

if (document.forms[0].SQ.value == "" || document.forms[0].SQ.value == "none") {
    window.alert("Please choose a security question.");
    return false;}

if (document.forms[0].sq_answer.value == "Question Answer"|| document.forms[0].sq_answer.value == "") {
    window.alert("Please input a sequrity question answer.");
    return false;}

    for (var i = 0; i <= 1; ++i) {

    if (document.forms[0].SpecialOffers[i].checked ==  true) {
        Special offers = true;
        break;}
    }

    if (Special offers == false) {
        window.alert("You must select yes or no to special offers.");
        return false;
    }


    for (var k = 0; k <= 3; k++) {
    if (document.forms[0].Offers[k].checked == true) {
        offers = true;
        break;}

    }
    if (offers == false) {
        window.alert("You must choose one offer.");
        return false;}

else
 return true;
 }

/* ]] */
</script>
</head>

<body>


<form  action="FormProcessor.html"  method = "get" enctype = "application/x-www-form-urlencoded" onsubmit="return confirmSubmit()">
<table  width="50%">
<tr><th colspan="2"><h1>Registration Page</h1></th></tr>

<tr><th colspan="2"><h2>Personal Information<h2></th></tr>
<tr><th colspan="2">(Fields with asterisks are mandatory)</th></tr>
<tr><td>* Name: </td>
<td><input type="text" name="name" value="Name" maxlength="20"  onFocus="doClear(this)" onkeyup="AcceptLetters(this)" /></td></tr>
<tr><td>* E-Mail:</td>
<td><input type="text" name="email" value="E-mail Address" onFocus="doClear(this)" /> </td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><th colspan="2"><h3>Security Information</h3></th></tr>
<tr><td>* Password:</td><td><input type="password" name="password1" /></td></tr>
<tr><td>* Confirm Password:</td><td><input type="password" name="password2" /></td></tr>
<tr><td>* Security Question:</td>
<td><select id="selection" name="SQ">
<option value="none">Please Choose One</option>
<option value="color">What is your favorite color?</option>
<option value="name">What is your Mother's maiden name?</option>
<option value="pet">What is your pets name?</option>
<option value="school">What high school did you graduate from?</option>
</select></td></tr>
<tr><td>Answer:</td><td><input type="text" name="sq_answer"  value="Question Answer" onFocus="doClear(this)" /></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><th colspan="2"><h4>* Preferences</h4></th></tr>

<tr><th colspan="2">Would you like any special offers sent to your E-mail address?</th></tr>
<tr><th colspan="2">Yes<input type="radio" name="SpecialOffers" value="Yes"/>No<input type="radio" name="SpecialOffers"  value="No" /></th></tr>
<tr><th colspan="2"><h5>Interests</h5></th></tr>
<tr><th colspan="2"><input type="checkbox" name="Offers" value="check"/>Entertainment</th></tr>
<tr><th colspan="2"><input type="checkbox" name="Offers" value="check"/>Shopping</th></tr>
<tr><th colspan="2"><input type="checkbox" name="Offers" value="check"/>Business</th></tr>
<tr><th colspan="2"><input type="checkbox" name="Offers" value="check"/>Exercise</th></tr>
<tr><td></td></tr>


<tr><th colspan="2"><input id="submit" type="submit" value="Submit"/><input id="reset" type="reset" value="Reset"/></th></tr>
</table>

</form>

</body>
</html>

【问题讨论】:

  • 您是否尝试过使用 Firebug 或 Chrome 开发工具调试您的代码?如果您的页面未显示内容或无法正常工作,则可能是您的脚本中某处存在错误,调试它会发现罪魁祸首。我在尝试使用您的页面时在 Chrome 中收到大量控制台错误。
  • 它正在正确显示内容。但它没有使用这些功能。
  • 这不是我们在这里所做的。我们不会在没有关于您为调试它所做的任何信息的情况下获取整段非工作代码并神奇地解决您的所有问题。这不是 StackOverflow 的工作方式。如果您正在对其进行故障排除并且您有一个可以提供详细信息的特定问题,那么您可能会得到一些帮助。首先要查看调试控制台中报告了哪些错误并从那里开始。
  • 附带说明,如果您的老师没有 (a) 教您如何使用调试工具,并且 (b) 无法帮助您追踪这些语法错误,我建议您更换老师.
  • 我并不是要你帮我修复我的代码,只是为了告诉我你是否看到了像 Arvind 那样向你尖叫的东西是错误的。

标签: javascript function


【解决方案1】:

您几乎没有语法错误:

Special offers 变量中不能有空格。

关键字不是retrun,而是return

【讨论】:

  • 请帮助教 OP 如何自己查看这些错误,而不是将我们用作语法检查器。
  • 我修复了语法错误,它仍然在做同样的事情,它仍然在提交表单而没有进行错误检查。此外,当我单击框时,该框应该被清除默认文本,但也没有完成
  • 是的,我发现返回错误并修复了语法错误并使其一切正常,我非常感谢您的帮助。
  • @Phibermatrix,很高兴它对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
  • 2017-04-18
  • 2011-12-30
  • 2018-02-10
  • 1970-01-01
相关资源
最近更新 更多