簡體   English   中英

JavaScript中不會顯示單選框和復選框的選中值

[英]Radio and Checkbox Checked Values Won't Show in JavaScript

我試圖使用JavaScript在一個新窗口中顯示單選按鈕和多個復選框的值。 我似乎無法弄清楚為什么返回“ true”而不是檢查的實際值。 以下是結果的代碼和屏幕截圖。

在此處輸入圖片說明

 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()方法都返回true ,這就是為什么字符串正確的原因。 您應該返回msg ,您的問題將得到解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM