天天看點

js表單各checkbox值

<html> 

<head> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 

<title>js表單各checkbox值</title> 

</head> 

<script language="javascript"> 

function aa(){ 

    var r=document.getElementsByName("r");  

    for(var i=0;i<r.length;i++){ 

        if(r[i].checked){ 

            alert(r[i].value+","+r[i].nextSibling.nodeValue); 

        } 

    }  

</script> 

<body> 

<form name="form1" method="post" action=""> 

<input type="checkbox" name="r" value="1">a<br> 

<input type="checkbox" name="r" value="2">b<br> 

<input type="checkbox" name="r" value="3">c<br> 

<input type="checkbox" name="r" value="4">d<br> 

<input type="checkbox" name="r" value="5">e<br> 

<input type="checkbox" name="r" value="6">f<br> 

<input type="checkbox" name="r" value="7">g<br> 

<input type="checkbox" name="r" value="8">h<br> 

<input type="checkbox" name="r" value="9">i<br> 

<input type="checkbox" name="r" value="10">j<br> 

<br> 

<input type="button" onclick="aa()" value="button"> 

</form> 

</body> 

</html> 

<b>nextSibling</b> 屬性可傳回某個元素之後緊跟的元素(處于同一樹層級中)。 如果無此節點,則屬性傳回 null。

nodeValue 屬性根據節點的類型設定或傳回節點的值。

      本文轉自許琴 51CTO部落格,原文連結:http://blog.51cto.com/xuqin/946317,如需轉載請自行聯系原作者

繼續閱讀