The c-sharp+JQurey implementation gets the values of radio and checkbox

  • 2020-03-30 04:30:09
  • OfStack

Without further ado, go straight to the code


<%@ 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 = " Zhang SAN: " + $("#name").val();
var str2 = " Gender: " + $('input:radio[name = "sex"]:checked').val();
var shuzu = [];
$('input:checkbox[name = "aihao"]:checked').each(function () {//Each is
shuzu.push($(this).val());//Assign an array value
});
window.alert(str1 + 'n' + str2 + 'n' + " Hobbies: " + shuzu);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
Name: <input type="text" id="name" /><br />
Gender: <input type="radio" id="nan" name="sex" checked="" value=" male " /> male <input type="radio" id="nv" name="sex" value=" female " /> female <br />
Hobbies: <input type="checkbox" /> swimming
<input type="checkbox" name="aihao" value=" Climbing the mountain " /> Climbing the mountain
<input type="checkbox" name="aihao" value=" Play a ball game " /> Play a ball game
<input type="checkbox" name="aihao" value=" Ride a bike " /> Ride a bike
<input type="checkbox" name="aihao" value=" reading " /> reading
<input type="checkbox" name="aihao" value=" chat " /> chat <br />
<input type="button" id="sub" value=" submit " /><input type="button" id="b" value=" reset " />
</form>
</body>
</html>

The code is very simple, but very practical, friends according to their own project requirements can be used.


Related articles: