我尝试使用JavaScript获取单选按钮和多个复选框的值以显示在新窗口中。我似乎无法弄清楚为什么"真的"返回而不是检查的实际值。以下是结果的代码和截图。
function messagebox()
{
var newWindow;
var msg ="";
var i = document.PizzaForm.state.options.selectedIndex;
var text = document.PizzaForm.state.options[i].text;
var value = document.PizzaForm.state.options[i].value;
{ document.PizzaForm.customer.value = document.PizzaForm.customer.value.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); }
{ document.PizzaForm.city.value = document.PizzaForm.city.value.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); }
newWindow = window.open("","","status=no,height=500,width=500");
message = "<ul><li>Name: " + document.PizzaForm.customer.value;
message += "<li>Address: " + document.PizzaForm.address.value;
message += "<li>City: " + document.PizzaForm.city.value;
message += "<li>State: " + document.PizzaForm.state.value;
message += "<li>Zip Code: " + document.PizzaForm.zip.value;
message += "<li>Phone: " + document.PizzaForm.phone.value;
message += "<li>Email: " + document.PizzaForm.email.value;
message += "<li>Size: " + showSize();
message += "<li>Toppings: " + showToppings();
newWindow.document.write(message);
function showSize()
{
for(i=0;i<document.PizzaForm.sizes.length;i++)
if(document.PizzaForm.sizes[i].checked)
msg += document.PizzaForm.sizes[i].value;
return true;
}
function showToppings()
{
for(i=0;i<document.PizzaForm.toppings.length;i++)
if(document.PizzaForm.toppings[i].checked)
msg += (i==0?"":",")+document.PizzaForm.toppings[i].value;
return true;
}
}
&#13;
答案 0 :(得分:0)
showSize()
和showToppings()
方法最终都返回true
,这就是你在字符串中获得真实的原因。你应该返回msg
,你的问题就会得到解决。