jquery获取复选框checkbox的值的简单实现方法

网络编程 发布日期:2024/10/4 浏览次数:1

正在浏览: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中  
      }

jquery:

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

其中jsp页面代码

<%@ 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="/UploadFiles/2021-04-02/jquery-1.7.2.js">

以上这篇jquery获取复选框checkbox的值的简单实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。