1. 程式人生 > >jquery獲取複選框checkbox的值

jquery獲取複選框checkbox的值

<%@ 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 //
%>

<!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>