jquery获取复选框checkbox的值

jQuery API :
each(callback) :以每一个匹配的元素作为上下文来执行一个函数。

:checked :匹配所有选中的被选中元素(复选框、单选框等,不包括select中的option)

Js代码  收藏代码
//js获取复选框值
            var obj = document.getElementsByName("interest");//选择所有name="interest"的对象,返回数组
            var s=‘‘;//如果这样定义var s;变量s中会默认被赋个null值
            for(var i=0;i<obj.length;i++){
                 if(obj[i].checked) //取到对象数组后,我们来循环检测它是不是被选中
                 s+=obj[i].value+‘,‘;   //如果选中,将value添加到变量s中
            }  

Js代码  收藏代码
//jquery获取复选框值
            var chk_value =[];//定义一个数组
            $(‘input[name="interest"]:checked‘).each(function(){//遍历每一个名字为interest的复选框,其中选中的执行函数
            chk_value.push($(this).val());//将选中的值添加到数组chk_value中
            });  

Html代码  收藏代码
<%@ page language="java" contentType="text/html" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//     basePath = http                 ://  127.0.0.1                :  8080                    /DWR_checkbox /
%>  

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">  

    <title>DWR获取浏览器页面信息</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
        <script type="text/javascript" src="jquery-1.7.2.js"></script>
        <script type=‘text/javascript‘ src=‘<%=path%>/dwr/engine.js‘></script>
        <script type=‘text/javascript‘ src=‘<%=path%>/dwr/util.js‘></script>
        <script type=‘text/javascript‘ src=‘<%=path%>/dwr/interface/test.js‘></script>
        <script type=‘text/javascript‘ src=‘<%=path%>/dwr/interface/test1.js‘></script>
        <script type=‘text/javascript‘ src=‘<%=path%>/dwr/interface/userLogin.js‘></script>
    <script type="text/javascript">   

        function ceshi1()
        {
            test.hasPermission(mydwr("user").value,mydwr("pass").value,  // 使用$()属性获取当前页面输入的用户名和权限的值
                function(data)
                {
                    if(data)
                    {
                        mydwr("hp1").checked = "checked";
                    }else{
                        mydwr("hp1").checked = null;
                    }
                    document.getElementById("boolean1").value = data;
                });
        }  

        function ceshi2()
        {
            test.hasPermission(dwr.util.getValue("username"),dwr.util.getValue("password"),// 使用DWR中的util.js工具中的属性获取当前页面输入的用户名和权限的值,给后台.java的hasPermission方法的参数赋值,再执行此方法(不是void类型有返回值)得到个返回值。
                function(data)
                {
                    if(data)
                    {
                        document.getElementById("hp").checked = "checked"; // 使用byId()属性获取当前页面checkbox的checked属性
                    }else{
                        document.getElementById("hp").checked = null;
                    }
                    document.getElementById("boolean2").value = data;
                    dwr.util.setValue("boolean3",data);
                    //dwr.util.setValue(boolean3,"哈哈");
                    dwr.util.setValue(div,data);
                    //dwr.util.setValue(body,data);
                });  // 用function(data)方法来处理后台.java方法执行后的返回值,存在data变量中,在执行function(data)方法的语句
        }  

        function invoke1()
        {
            dwr.engine.setAsync(false);//<!-- 默认是异步执行的true,设置成false就是同步执行 方法按顺序执行-->
            test1.method1(
                function(data){
                    alert(data);
                }
            );
            test1.method2(
                function(data){
                    alert(data);
                }
            );
        }  

        function invoke2(){
            test.getArray(
                function(data)
                {
                    //for(var i=0;i<data.length;i++){
                    //  alert(data[i]);
                    //}
                    dwr.util.addOptions(selectid,data);//根据后台数组值填充ID为selectid的列表
                });
         }
         function invoke3(){
            dwr.util.removeAllRows(tid);//根据tbody的id删除该tbody
         }  

         function invoke4(){
            var a=dwr.util.getText(selectid);
            dwr.util.setValue(tdid,a);
         }
        function show()
        {
            var name = document.getElementById("user").value;
            var pass = document.getElementById("pass").value;
            var obj = new objBean(name,pass);
            userLogin.alterUser(obj,
                function(data){
                    if(name == data.username && pass == data.password){
                        alert("success");
                    }else{
                        alert("error");
                    }
                    document.getElementById("user").value = data.username;
                    document.getElementById("pass").value = data.password;
            });
        }
        function objBean(name,pass)
        {
            this.username = name;
            this.password = pass;
        }
        function go_to()
        {
            $(‘#first_jsp‘).show();
            test.getInclude(function(data){
                $(‘#first_jsp‘).html(data);
            });
        }
        function go_to_iframe()
        {
            $("#myframe1").show();
            test.getIncludeBean(function(data){
                $(‘#myframe1‘).attr(‘src‘,"<%=basePath%>"+data);
            });
        }
        function getInfo(){
            //js获取复选框值
            var obj = document.getElementsByName("interest");//选择所有name="interest"的对象,返回数组
            var s=‘‘;//如果这样定义var s;变量s中会默认被赋个null值
            for(var i=0;i<obj.length;i++){
                 if(obj[i].checked) //取到对象数组后,我们来循环检测它是不是被选中
                 s+=obj[i].value+‘,‘;   //如果选中,将value添加到变量s中
            }
            alert(s == ‘‘ ? ‘你还没有选择任何内容!‘ :s);  

            dwr.util.setValue(tdid,s);
            //jquery获取复选框值
            var chk_value =[];//定义一个数组
            $(‘input[name="interest"]:checked‘).each(function(){//遍历每一个名字为interest的复选框,其中选中的执行函数
            chk_value.push($(this).val());//将选中的值添加到数组chk_value中
            });
            alert(chk_value.length==0 ?‘你还没有选择任何内容!‘:chk_value);
            dwr.util.setValue(checkboxInfo,chk_value);
        }
    </script>
  </head>
  <body id="body">
  <form>
    <table id="tableid" border="1" align="center">
        <tr><td>用户名:</td><td><input id="user" type="text" name="username"/></td></tr>
        <tr><td>密码:</td><td><input id="pass" type="text" name="password"/></td></tr>
        <tr>
            <td><input id="getInfo" type="button" value="获取信息" onclick="show()"/></td>
            <td><input type="reset" value="重置" /></td>
        </tr>
        <tbody id="tid">
            <tr>
                <td colspan="2" >
                    <input id="hp1" type="checkbox" name="hpname" >测试权限<br>
                    <input type="button"name="button"value="测试1" onclick="ceshi1()">
                    返回值:<input id="boolean1" type="text" />
                </td>
            </tr>
            <tr>
                <td >
                    <input id="hp" type="checkbox" name="hpname" >测试权限<br>
                    <input type="button"name="button"value="测试2" onclick="ceshi2()">
                </td>
                <td>
                    返回值:<input id="boolean2" type="text" />
                    dwr.util.setValue:<input id="boolean3" type="text" />
                    <div id="div" > 这是一个div标签</div>
                </td>
            </tr>
        </tbody>
        <tr>
            <td id="tdid"colspan="2" >修改此行值</td>
        </tr>
    </table>
    </form>
                <input type="button"name="button"value="异步调用测试" onclick="invoke1()">
                <input type="button"name="button"value="加载Array值" onclick="invoke2()">
                <input type="button"name="button"value="删除所有行" onclick="invoke3()">
                <input type="button"name="button"value="修改行值" onclick="invoke4()">
        <div>
            <select id="selectid"></select>
        </div>
        <input type="button"name="button"value="框架(iframe)中加载bean.jsp" onclick="go_to_iframe()">
        <input type="button"name="button"value="DIV中加载first.jsp" onclick="go_to()">
        <iframe id="myframe1" style="width:500;height:200;border:10px;padding:0px;display:none"  src=""  ></iframe>
        <div id="first_jsp" style="width: 100%; height: auto; display:none"></div>
<form>
        <input type="checkbox" name="interest" value="VC" />VC
        <input type="checkbox" name="interest" value="VB" />VB
        <input type="checkbox" name="interest" value="VFoxpro" />VFoxpro
        <input type="checkbox" name="interest" value="VJava" />VJava
        <input type="checkbox" name="interest" value="BC" />BC
        <input type="checkbox" name="interest" value="Cobol" />COobol
        <input type="checkbox" name="interest" value="Java" />Java
        <input type="checkbox" name="interest" value="Delphi" />Delphi
        <input type="button" value="获取复选框值" onclick="getInfo()">
</form>
        <div id="checkboxInfo" style="width: 100%; height: auto; display:block"></div>
  </body>
</html>  
时间: 2024-08-05 12:58:04

jquery获取复选框checkbox的值的相关文章

jquery操作复选框(checkbox)的12个小技巧总结

1.获取单个checkbox选中项(三种写法)$("input:checkbox:checked").val()或者$("input:[type='checkbox']:checked").val();或者$("input:[name='ck']:checked").val(); 2. 获取多个checkbox选中项$('input:checkbox').each(function() {        if ($(this).attr('che

Jquery获取复选框的方法

转自:http://blog.csdn.net/longyangyangyang/article/details/6128141 html代码: <input type="checkbox" name="test" value="0" />0   <input type="checkbox" name="test" value="1" />1   <inpu

不同版本的jquery的复选框checkbox的相关问题

在尝试写复选框时候遇到一个问题,调试了很久都没调试出来,极其郁闷: IE10,Chrome,FF中,对于选中状态,第一次$('#checkbox').attr('checked',true)可以实现 但是当通过代码清除选中,下次再通过代码 $('#checkbox').attr('checked',true) 去选中时 虽然代码中有checked='checked',但是画面表现都没有打勾. 例如如下的代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML

jQuery 操作复选框(checkbox) attr checked不起作用

jQuery 更改checkbox的状态,无效 $(this).attr("checked", false).checkboxradio("refresh");     //应该这么写吧,少了$这个东东~~~跟js混淆了 jQuery 操作复选框(checkbox) attr checked不起作用 这 天用到jQuery功能,想实现一个简单的复选框动态全选或全不选,结果测试发现 attr(‘checked’,'checked’);与attr(‘checked’,t

jquery 获取一组元素的选中项 - 函数、jquery获取复选框值、jquery获取单选按钮值

做表单提交时,如果现在还在用form提交,用户体验很差,所以一般使用ajax提交. 其中需要获取每个表单输入元素的值,获取的时候像文本框这些还好说,Jquery提供了 .val() 方法,获取很方便,但是获取复选框和单选按钮的值确比较麻烦. 今天闲来无事封装了一个函数,以后获取复选框和单选按钮的值就只需要调用这个函数,传入jquery选择器就可以了,下面附上函数. 函数 /** * 获取单个或一组输入元素的选中项 * 传入Jquery选择器 * 支持:text,返回:文本 * 支持:hidden

Jquery操作复选框(CheckBox)的取值赋值实现代码

赋值 复选框 CheckBox 遍历 取值 1. 获取单个checkbox选中项(三种写法): $("input:checkbox:checked").val() 或者 $("input:[type='checkbox']:checked").val(); 或者 $("input:[name='ck']:checked").val(); 复制代码 2. 获取多个checkbox选中项: $('input:checkbox').each(funct

jQuery——操作复选框(checkbox) attr checked不起作用

这天用到jQuery功能,想实现一个简单的复选框动态全选或全不选,结果测试发现 attr('checked','checked');与attr('checked',true); 都不好使,要么第一次成功了,第二次调用就没反应,完全不起作用了.那到底是什么原因呢? 害得'跑客教授'到处到网上搜 jQuery checkbox的操作,动态选择的相关文章,都写着是这样的实现代码 $('input[type=checkbox]').attr('checked','checked');//全选,设置属性

jquery获取复选框(checkbox)的选中值(数组或者单个)

普及jquery的each方法以及javascript的两个数组操作函数push和join each() 方法规定为每个匹配元素规定运行的函数. 语法 $(selector).each(function(index,element)) index - 选择器的 index 位置 element - 当前的元素(也可使用 "this" 选择器) push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度. 语法 arrayObject.push(newelement1,newel

jquery获取复选框的值

勾选checkbox,并把勾选的值显示在某个div中 1 <!DOCTYPE html > 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title> 获取爱好 </title> 6 <meta http-equiv = "content-type" content ="text/html;charset=utf-8" /&