廢話不多說,直接上代碼
代碼如下:
<%@ 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="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="男" />男<input type="radio" id="nv" name="sex" value="女" />女<br />
愛好:<input type="checkbox" />游泳
<input type="checkbox" name="aihao" value="爬山" />爬山
<input type="checkbox" name="aihao" value="打球" />打球
<input type="checkbox" name="aihao" value="騎車" />騎車
<input type="checkbox" name="aihao" value="閱讀" />閱讀
<input type="checkbox" name="aihao" value="聊天" />聊天<br />
<input type="button" id="sub" value="提交" /><input type="button" id="b" value="重置" />
</form>
</body>
</html>
代碼很簡潔,卻很實用,小伙伴們根據自己的項目需求改寫下即可使用。