0
這段代碼似乎正在做我想做的事,直到用戶單擊提交的部分和validateInput()函數應該提交表單或發送錯誤警報消息。 我無法弄清楚代碼有什麼問題。使用提交按鈕功能在javascript中調試問題
任何人都可以幫忙嗎?
這是鏈接到實際的代碼 - http://ciswebs.smc.edu/cis55/naruse_kevin_k/week2/default.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Week 2 cis 55</title>
<script type="text/javascript">
function validateText(inputField, helpText)
{
if (inputField.value.length == 0)
{
if (helpText != null)
{
helpText.innerHTML = "please enter a value";
}
return false;
}
else
{
if (helpText != null)
{
helpText.innerHTML = /*inputField.value.length + */"";
}
return true;
}
}
function validate_zip_code(inputField, helpText)
{
var isnumber = true; var is5chars = true;
if (isNaN(inputField.value))
{
//alert("not a number");
if (helpText != null)
{
helpText.innerHTML = /*inputField.value.length +*/ "please enter a numeric value";
}
return false;
}
else if (inputField.value.length != 5)
{
//alert("not 5 digits")
if (helpText != null)
{
helpText.innerHTML = /*inputField.value.length +*/ "please enter a valid zip code with 5 digits";
}
return false;
}
else
{
if (helpText != null)
{
helpText.innerHTML = "";
}
//alert("passed the test");
return true;
}
}
function submit_validate(form)
{ //alert("check everything except zip");
if (validateText(form["first_name"], form["first_name_help"]) &&
validateText(form["last_name"], form["last_name_help"]) &&
validateText(form["street"], form["street_help"]) &&
validateText(form["city"], form["city_help"]) &&
validateText(form["state"], form["state_help"]) &&
validate_zip_code(form["zip"], form["zip_help"])
)
{
alert("so far - validated everyting else next check zip code");
// on this next if/then
alert("yay - ready to submit now");
form.submit();
}
else
{ alert("we didn't even get to zip, smething else is wrong");
alert("one or more fields were incorect. please correct before submitting");
return false;
}
}
function submit_validate1(form)
{ //alert("check everything except zip");
if (validateText(form["first_name"], form["first_name_help"]) &&
validateText(form["last_name"], form["last_name_help"]) &&
validateText(form["street"], form["street_help"]) &&
validateText(form["city"], form["city_help"]) &&
validateText(form["state"], form["state_help"])
)
{
//alert("so far - validated everyting else next check zip code");
// on this next if/then
if (validate_zip_code(form["zip"], form["zip_help"]))
{
form.submit();
alert("yay - ready to submit now");
}
else
{
alert("your zip isn't valid - please correct the info and resubmit");
}
}
else
{ alert("we didn't even get to zip, smething else is wrong");
alert("one or more fields were incorect. please correct before submitting");
}
}
</script>
</head>
<body
<form action="" method="post" name="address_form">
<fieldset>
<p>
<label>First Name:</label>
<input name="first_name" id="first_name" type="text"
onblur="validateText(this, document.getElementById('first_name_help'));" />
<span id="first_name_help" class="help"></span>
</p>
<p>
<label>Last Name:</label>
<input name="last_name" id="last_name" type="text"
onblur="validateText(this, document.getElementById('last_name_help'));">
<span id="last_name_help" class="help"></span>
</p>
<p>
<label>Street</label>
<input name="street" id="street" type="text"
onblur="validateText(this, document.getElementById('street_help'));">
<span id="street_help" class="help"></span>
</p>
<p>
<label>City</label>
<input name="city" id="city" type="text"
onblur="validateText(this, document.getElementById('city_help'));">
<span id="city_help" class="help"></span>
</p>
<p>
<label>State</label>
<input name="state" id="state" type="text"
onblur="validateText(this, document.getElementById('state_help'));">
<span id="state_help" class="help"></span>
</p>
<p>
<label>Zip</label>
<input name="zip" type="text"
onblur="validate_zip_code(this, document.getElementById('zip_help'));">
<span id="zip_help" class="help"></span>
</p>
<p>
<input name="submit" type="submit" id="submit" value="submit"
onclick="return submit_validate(this.form);">
</p>
</fieldset>
</form>
</body>
</html>
您是否嘗試過使用開發人員工具對其進行調試瀏覽器。 – 2014-10-08 00:26:44
將點擊監聽器從提交按鈕移動到表單的onsubmit處理程序,可以在不點擊提交按鈕的情況下提交表單。從提交按鈕中刪除名稱和ID(名稱或ID爲「submit」的表單控件會掩蓋表單的提交方法,因此您無法調用它)。 – RobG 2014-10-08 00:38:24