首页 > 代码库 > JQurey获取radio和checkbox的值
JQurey获取radio和checkbox的值
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Web2.aspx.cs" Inherits="Chapter2.Web2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="http://www.mamicode.com/jquery-1.8.0.js"></script>
<script type="text/javascript">
$(function () {
$("#sub").click(function () {
var str1 = "张三:" + $("#name").val();
var str2 = "性别:" + $(‘input:radio[name = "sex"]:checked‘).val();
var shuzu = [];
$(‘input:checkbox[name = "aihao"]:checked‘).each(function () {//each每个
shuzu.push($(this).val());//给数组赋值
});
window.alert(str1 + ‘\n‘ + str2 + ‘\n‘ + "爱好:" + shuzu);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
姓名:<input type="text" id="name" /><br />
性别:<input type="radio" id="nan" name="sex" checked="" value="http://www.mamicode.com/男" />男<input type="radio" id="nv" name="sex" value="http://www.mamicode.com/女" />女<br />
爱好:<input type="checkbox" />游泳
<input type="checkbox" name="aihao" value="http://www.mamicode.com/爬山" />爬山
<input type="checkbox" name="aihao" value="http://www.mamicode.com/打球" />打球
<input type="checkbox" name="aihao" value="http://www.mamicode.com/骑车" />骑车
<input type="checkbox" name="aihao" value="http://www.mamicode.com/阅读" />阅读
<input type="checkbox" name="aihao" value="http://www.mamicode.com/聊天" />聊天<br />
<input type="button" id="sub" value="http://www.mamicode.com/提交" /><input type="button" id="b" value="http://www.mamicode.com/重置" />
</form>
</body>
</html>
JQurey获取radio和checkbox的值