js中获得checkbox里选中的多个值

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Programmer_FuQiang/article/details/84020208

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>louis-blog >> jQuery 对checkbox的操作</title> 
    <mce:script type='text/javascript' src="http://leotheme.cn/wp-includes/js/jquery/jquery.js" mce_src="http://leotheme.cn/wp-includes/js/jquery/jquery.js"></mce:script> 
</head> 
<body style="text-align:center;margin: 0 auto;font-size: 12px;" mce_style="text-align:center;margin: 0 auto;font-size: 12px;"> 
<div style="border: 1px solid #999; width: 500px; padding: 15px; background: #eee; margin-top: 150px;"> 

<form name="form1" method="post" action=""> 
<input type="checkbox" name="test" value="橘子"/><span>1</span>
<input type="checkbox" name="test" value="桃子"/><span>2</span>
<input type="checkbox" name="test" value="苹果"/><span>3</span>
<input type="checkbox" name="test" value="鸭梨"/><span>4</span>
<input type="checkbox" name="test" value="葡萄"/><span>5</span>
<input type='button' value='提交' onclick="fun()"/>

</form> 
</div> 
<script>
function fun(){
    obj = document.getElementsByName("test");
    check_val = [];
    for(k in obj){
        if(obj[k].checked)
            check_val.push(obj[k].value);
    }
    alert(check_val);
}

</script>
</body> 
</HTML> 

猜你喜欢

转载自blog.csdn.net/Programmer_FuQiang/article/details/84020208
今日推荐