checkbox使用javascript判斷

edited 一月 2014 in Javascript
要用javascript來判斷以下的Business、People、Product的checkbox是否有勾選可以怎麼寫?

求大大指點

Business
<input name="business[]"  type="checkbox" value="1">
<input name="business[]"  type="checkbox" value="2">
<input name="business[]"  type="checkbox" value="3">
People
<input name="people[]"  type="checkbox" value="1">
<input name="people[]"  type="checkbox" value="2">
<input name="people[]"  type="checkbox" value="3">
Product
<input name="product[]"  type="checkbox" value="1">
<input name="product[]"  type="checkbox" value="2">
<input name="product[]"  type="checkbox" value="3">

評論

  • 純Javascipt
    <input type='button' onClick='test()'>
    <script type='text/javascript'>
    	function test(){
    		var product = document.getElementsByName('product[]');
    		var count_checked = 0;
    		for(var i=0; i<product.length; i++){
    			if(product[i].checked){
    				count_checked++;
    			}
    		}
    		alert(count_checked);
    	}
    </script>
    
    有基礎的話, 建議可以學jQuery
Sign In or Register to comment.