JS如何獲取表單中復選框的值?


<!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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM