使用JQuery获取form表单的值在控制台打印

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf­8">
		<title></title>
		<script src="js/jquery-3.6.0.js"></script>
		<style type="text/css">
			body {
				margin: 0px; font­size: 14px;
			}

			.top {
				width: 100%; height: 40px; background­color: azure; border: 0px solid red; position: relative;
			}

			.top span {
				font­weight: bold; float: left; line­height: 40px; margin­left: 7px;
			}

			.topul {
				/*设置为none会把ul中实心圆圈去掉*/
				margin: 0px; list­style: none;
			}

			.topul li {
				float: left; line­height: 40px; margin­left: 5px; margin­right: 3px;
			}

			.main {
				margin: 10px 18px;
			}

			.main_title {
				height: 30px; margin­top: 10px; margin­bottom: 20px;
					/* border­bottom­color: gainsboro;
					border­bottom­style: solid;
					border­bottom­width: 1px; */
					border­bottom: 1px solid gainsboro; position: relative;
			}

			.main_title span {
				font­weight: bolder; border­bottom: 3px solid #00FFFF; line­height: 30px; position: absolute; bottom: 1px;
			}

			.main_body {
				width: 100%;
			}

			.main_body ul {
				list­style: none;
			}

			.main_body ul li {
				margin­bottom: 20px; position: relative;
			}

			label {
				width: 80px; border: 0px solid red; display: inline­block; line­height: 28px; text­align: center; float: left;
			}

			input[name="userName"],
			input[name=pwd] {
				width: 300px; line­height: 28px;
					/*边框圆角*/
					border­radius: 5px; border: 1px solid #A9A9A9;
			}

			select {
				width: 160px; width: 160px; height: 28px; border­radius: 5px; border: 1px solid #A9A9A9;
			}

			textarea {
				width: 600px; height: 100px; border­radius: 5px; border: 1px solid #A9A9A9;
			}

			input[type="button"] {
				width: 100px; text­align: center; line­height: 28px; background­color: darkturquoise; border: 0px solid white; color: white; border­radius: 5px;
			}

			input[type="radio"],
			input[type="checkbox"] {
				margin­top: 9px;
			}
		</style>
		<script type="text/javascript">
			//获取表单数据
			$(function(){
				$("#btn").click(function(){
					var userName = $("#userName").val();
					var pwd = $("#pwd").val();
					
					var sex = $("input[name='sex']:checked").val();
					
					var hobby = [];
					var hobbys = $("input[name='hobby']:checked");
					for(var i = 0; i < hobbys.length; i++ ){
						hobby[i] = $(hobbys[i]).val();
					}
					
					var star = $("select option:selected").html();
					
					var remark = $("#remark").val();
					
					var results = {};
					results = {userName:userName, pwd:pwd, sex:sex, hobby:hobby, star:star, remark:remark};
					console.log(results);
				})
			})
		</script>
	</head>
	<body>
		<div class="top">
			<span>位置:</span>
			<ul class="topul">
				<li>首页</li>
				<li>­­></li>
				<li>表单</li>
			</ul>
		</div>
		<div class="main">
			<div class="main_title">
				<span>注册信息</span>
			</div>
			<form>
				<div class="main_body">
					<ul>
						<li>
							<label>账号</label>
							<input id="userName" type="text" name="userName" placeholder="请输入账号" />
						</li>
						<li>
							<label>密码</label>
							<input id="pwd" type="password" name="pwd" placeholder="请输入密码" />
						</li>
						<li>
							<label>性别</label>
							<input type="radio" name="sex" value="male" />男
							<input type="radio" name="sex" value="female" />女
						</li>
						<li>
							<label>爱好</label>
							<input type="checkbox" name="hobby" value="sing" />唱歌
							<input type="checkbox" name="hobby" value="dance" />跳舞
							<input type="checkbox" name="hobby" value="football" />足球
						</li>
						<li>
							<label>星座</label>
							<select name="star" id="star">
								<option value="1">白羊座</option>
								<option value="2">狮子座</option>
								<option value="3">巨蟹座</option>
							</select>
						</li>
						<li>
							<label>备注</label>
							<textarea id="remark" name="remark"></textarea>
						</li>
						</li>
						<li>
							<label>&nbsp;</label>
							<input id="btn" type="button" name="submit" value="注&nbsp;册" />
						</li>
					</ul>
				</div>
			</form>
			<p id="result"></p>
		</div>
	</body>
</html>

浏览器运行页面

提交后控制台打印结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值