JavaScript 中不会显示单选框和复选框选中的值

Radio and Checkbox Checked Values Won't Show in JavaScript

我正在尝试使用 JavaScript 获取单选按钮和多个复选框的值以显示在新的 window 中。我似乎无法弄清楚为什么 "true" return 而不是检查的实际值。下面是结果的代码和截图。

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;
 } 
}

showSize()showToppings() 方法最终都是 returning true,这就是为什么您在字符串中得到 true 的原因。你应该 return msg 你的问题就会得到解决。