<!DOCTYPE html>
<html>
<head>
	<title>表单轮播</title>
	<style>
		body {
			background-color: green;
		}
		form {
			border: 1px solid black;
			padding: 10px;
			margin: 50px auto;
			max-width: 500px;
			background-color: white;
		}
		label {
			display: block;
			margin-bottom: 10px;
		}
		input[type="submit"] {
			margin-top: 10px;
			padding: 5px 10px;
			background-color: #4CAF50;
			color: white;
			border: none;
			border-radius: 5px;
			cursor: pointer;
		}
	</style>
</head>
<body>
	<form id="myForm">
		<label>
			姓名:
			<input type="text" name="name" required>
		</label>
		<label>
			性别:
			<input type="radio" name="gender" value="male" checked>男
			<input type="radio" name="gender" value="female">女
		</label>
		<label>
			爱好:
			<input type="checkbox" name="hobby" value="reading">阅读
			<input type="checkbox" name="hobby" value="music">音乐
			<input type="checkbox" name="hobby" value="sports">运动
			<input type="checkbox" name="hobby" value="travel">旅行
		</label>
		<label>
			邮箱:
			<input type="email" name="email" required>
		</label>
		<label>
			手机号码:
			<input type="tel" name="phone" pattern="[0-9]{11}" required>
		</label>
		<label>
			简介:
			<textarea name="intro"></textarea>
		</label>
		<input type="submit" value="提交">
	</form>
<pre><code>&lt;script&gt;
	var form = document.getElementById(&quot;myForm&quot;);
	var currentFieldset = 0;
	var fieldsets = form.getElementsByTagName(&quot;fieldset&quot;);

	// 隐藏所有fieldset
	for (var i = 1; i &lt; fieldsets.length; i++) {
		fieldsets[i].style.display = &quot;none&quot;;
	}

	form.addEventListener(&quot;submit&quot;, function(e) {
		e.preventDefault(); // 阻止表单提交

		// 显示下一个fieldset或提交表单
		if (currentFieldset &lt; fieldsets.length - 1) {
			fieldsets[currentFieldset].style.display = &quot;none&quot;;
			currentFieldset++;
			fieldsets[currentFieldset].style.display = &quot;block&quot;;
		} else {
			alert(&quot;表单已提交!&quot;);
			form.submit();
		}
	});
&lt;/script&gt;
</code></pre>
</body>
</html>

原文地址: https://www.cveoy.top/t/topic/7Df 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录