【发布时间】:2015-08-08 20:59:54
【问题描述】:
我正在处理一项任务,但遇到了困难。
我正在尝试在提交后在警报框弹出屏幕中显示浇头的复选框值。文本框和单选值出现,但复选框返回空白值。
知道我做错了什么吗?
<html>
<head>
<title>HTML and JavaScript</title>
<script>
function doClear()
{
document.PizzaForm.customer.value ="";
document.PizzaForm.address.value = "";
document.PizzaForm.city.value = "";
document.PizzaForm.state.value="";
document.PizzaForm.zip.value="";
document.PizzaForm.phone.value="";
document.PizzaForm.email.value="";
document.PizzaForm.sizes[0].checked = false;
document.PizzaForm.sizes[1].checked = false;
document.PizzaForm.sizes[2].checked = false;
document.PizzaForm.sizes[3].checked = false;
document.PizzaForm.toppings[0].checked = false;
document.PizzaForm.toppings[1].checked = false;
document.PizzaForm.toppings[2].checked = false;
document.PizzaForm.toppings[3].checked = false;
document.PizzaForm.toppings[4].checked = false;
document.PizzaForm.toppings[5].checked = false;
document.PizzaForm.toppings[6].checked = false;
document.PizzaForm.toppings[7].checked = false;
document.PizzaForm.toppings[8].checked = false;
return;
}
function doSubmit()
{
if(validateText() == true);
if(validateRadio() == true);
if(validateCheck() == true);
alert("Name:" + " " + document.PizzaForm.customer.value +
'\n' +
"Address:" + " " + document.PizzaForm.address.value +
'\n' +
"City:" + " " + document.PizzaForm.city.value +
'\n' +
"State:" + " " + document.PizzaForm.state.value +
'\n' +
"Zip:" + " " + document.PizzaForm.zip.value +
'\n' +
"Phone:" + " " + document.PizzaForm.phone.value +
'\n' +
"Email:" + " " + document.PizzaForm.email.value +
'\n' +
"Size:" + " " + document.PizzaForm.sizes.value +
'\n' +
"Toppings:" + " " + document.PizzaForm.toppings.value);
return;
}
function validateText()
{
var customer = document.PizzaForm.customer.value;
if (customer.length == 0)
{
alert("Please enter your name.")
}
var address = document.PizzaForm.address.value;
if (address.length == 0)
{
alert("Please enter an address.")
}
var city = document.PizzaForm.city.value;
if (city.length == 0)
{
alert("Please enter a city.")
}
var state = document.PizzaForm.state.value;
if (state.length == 0)
{
alert("Please enter a state.")
}
var zip = document.PizzaForm.zip.value;
if (zip.length == 0)
{
alert("Please enter a zip code.")
}
var phone = document.PizzaForm.phone.value;
if (phone.length == 0)
{
alert("Please enter a phone number.")
}
var email = document.PizzaForm.email.value;
atpos = email.indexOf("@");
dotpos = email.lastIndexOf(".");
if (atpos < 1 || ( dotpos - atpos < 2 ))
{
alert("Please enter an email address.")
return false;
}
return true;
}
function validateRadio()
{
if (document.PizzaForm.sizes[0].checked) return true;
if (document.PizzaForm.sizes[1].checked) return true;
if (document.PizzaForm.sizes[2].checked) return true;
if (document.PizzaForm.sizes[3].checked) return true;
if (document.PizzaForm.sizes.value == false);
{
alert("Please choose a pizza size.");
}
return;
}
function validateCheck()
{
if (document.PizzaForm.toppings[0].checked == false &&
document.PizzaForm.toppings[1].checked == false &&
document.PizzaForm.toppings[2].checked == false &&
document.PizzaForm.toppings[3].checked == false &&
document.PizzaForm.toppings[4].checked == false &&
document.PizzaForm.toppings[5].checked == false &&
document.PizzaForm.toppings[6].checked == false &&
document.PizzaForm.toppings[7].checked == false &&
document.PizzaForm.toppings[8].checked == false)
{
alert("Please pick a topping of your choice.");
}
return false;
return true;
}
</script>
</head>
<body>
<form name="PizzaForm">
<h1>The JavaScript Pizza Parlor</h1>
<p>
<h4>Step 1: Enter your name, address, and phone number:</h4>
<font face ="Courier New">
Name: <input name="customer" size="50" type="text"><br>
Address: <input name="address" size"50" type="text"><br>
City: <input name="city" size="15" type="text">
State: <input name="state" size="2" type="TEXT">
Zip: <input name="zip" size="5" type="text"><br>
Phone: <input name="phone" size="50" type="text"><br>
Email: <input name="email" size="40" type="text"><br>
</font>
</p>
<p>
<h4>Step 2: Select the size of pizza you want:</h4>
<font face="Courier New">
<input name="sizes" type="radio" value="Small">Small
<input name="sizes" type="radio" value="Medium">Medium
<input name="sizes" type="radio" value="Large">Large
<input name="sizes" type="radio" value="Jumbo">Jumbo<br>
</font>
</p>
<p>
<h4>Step 3: Select the pizza toppings you want:</h4>
<font face="Courier New">
<input name="toppings" type="checkbox" value="Pepperoni">Pepperoni
<input name="toppings" type="checkbox" value="Canadian Bacon">Canadian Bacon
<input name="toppings" type="checkbox" value="Sausage">Sausage<br>
<input name="toppings" type="checkbox" value="Mushrooms">Mushrooms
<input name="toppings" type="checkbox" value="Pineapple">Pineapple
<input name="toppings" type="checkbox" value="Black Olives">Black Olives<br>
<input name="toppings" type="checkbox" value="Green Peppers">Green Peppers
<input name="toppings" type="checkbox" value="Extra Cheese">Extra Cheese
<input name="toppings" type="checkbox" value="None">None<br>
</font>
</p>
<input type="button" value="Submit Order" onClick="doSubmit()">
<input type="button" value="Clear Entries" onClick="doClear()">
</form>
</body>
</html>
【问题讨论】:
-
你有太多不必要的代码和 if 条件。我建议您考虑使用类名和 for 循环。你会发现你的 90% 的 javascript 都放在了一个 if 条件中。
-
CheckboxElement.checked测试复选框的布尔值。显然,CheckboxElement.value是复选框的值。使用条件和循环。if(CheckboxElement.checked){someArray.push(CheckboxElement.value)}。当然,您需要定义 someArray。然后你可以稍后.join()它或其他东西。要么,要么你可以连接一个字符串。您的选择。
标签: javascript jquery html validation checkbox