<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <script> function f1() { //獲取所有name為chk的多選框,返回一個多選框陣列 var chks = document.form.chk; //把選中的多選框的值拼接成字串 var str = ""; //迴圈多選框陣列 for (var i = 0; i < chks.length; i++) { //如果多選框被選中,則把值累計到str中 if (chks[i].checked == true) { str += chks[i].value + ","; } } //彈出多選框選中的結果 alert("您勾選的值是:"+str); } </script></head><body> <form name="form"> <input type=checkbox name="chk" value=1> <input type=checkbox name="chk" value=2> <input type=checkbox name="chk" value=3> <input type="button" value="測試" onclick="f1()"/> </form></body></html>
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <script> function f1() { //獲取所有name為chk的多選框,返回一個多選框陣列 var chks = document.form.chk; //把選中的多選框的值拼接成字串 var str = ""; //迴圈多選框陣列 for (var i = 0; i < chks.length; i++) { //如果多選框被選中,則把值累計到str中 if (chks[i].checked == true) { str += chks[i].value + ","; } } //彈出多選框選中的結果 alert("您勾選的值是:"+str); } </script></head><body> <form name="form"> <input type=checkbox name="chk" value=1> <input type=checkbox name="chk" value=2> <input type=checkbox name="chk" value=3> <input type="button" value="測試" onclick="f1()"/> </form></body></html>