javascript - 选项卡在 jQuery 中不起作用?

标签 javascript jquery html css jquery-ui-tabs

我需要在预订表单(3 个标签)中创建标签,其中包含供用户填写然后提交的文本框。我似乎无法让它工作,但我不确定为什么?我是否将内容(文本框)放在了错误的位置?

$(document).ready(function() {
	
	//tabs function
	$("#reservation_tabs").tabs();
	
	var emailPattern = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b/;
		
	// add a span element after each text box
	$(":text").after("<span class='error'>*</span>");
	
	// move the focus to the first text box
	$("#arrival_date").focus();
	
	// the handler for the submit event of the form
	// executed when the submit button is clicked
	$("#reservation_form").submit(
		function(event) {
			var isValid = true;
			
			// validate the requested arrival date
			if ($("#arrival_date").val() == "") {
				$("#arrival_date").next().text("This field is required.");
				isValid = false;
			} else {
				$("#arrival_date").next().text("");				
			}
			
			// validate the number of nights
			if ($("#nights").val() == "") {
				$("#nights").next().text("This field is required.");
				isValid = false;
			} else if (isNaN($("#nights").val())) {
				$("#nights").next().text("This field must be numeric.");
				isValid = false;
			} else {
				$("#nights").next().text("");
			}		

			// validate the name entry
			var name = $("#name").val().trim();
			if (name == "") {
				$("#name").next().text("This field is required.");
				isValid = false;
			} 
			else {
				$("#name").val(name);
				$("#name").next().text("");
			}
						
			// validate the email entry with a regular expression
			var email = $("#email").val();
			if (email == "") { 
				$("#email").next().text("This field is required.");
				isValid = false;
			} else if ( !emailPattern.test(email) ) {
				$("#email").next().text("Must be a valid email address.");
				isValid = false;
			} else {
				$("#email").next().text("");
			} 
			
			// validate the phone number
			if ($("#phone").val() == "") { 
				$("#phone").next().text("This field is required.");
				isValid = false; 
			} else {
				$("#phone").next().text("");
			}
			
			// prevent the submission of the form if any entries are invalid 
			if (isValid == false) {
				event.preventDefault();				
			}
		} // end function
	);	// end submit
}); // end ready
body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 87.5%;
    background-color: white;
    margin: 0 auto;
    width: 600px;
    border: 3px solid blue;
    padding: 10px 20px;
}
legend {
    color: blue;
    font-weight: bold;
    margin-bottom: .8em;
}
label {
	float: left;
    width: 100px;
}
input, select {
    margin-left: 1em;
    margin-right: 1em;
    margin-bottom: .5em;
}
input {
    width: 14em;	
}
input.left {
	width: 1em;
	padding-left: 0;
}
fieldset {
	border: none;
	margin-left: 0;
	margin-top: 1em;
	padding: 0;
}
input.last {
	margin-bottom: 1em;
}
#adults, #children {
	width: 35px;
}
#smoking {
	width: 1em;
	margin-left: 0;
}
#policies {
	margin-left: 0;
	width: 15em;
}
#submit {
	width: 10em;
}
#dialog p {
	font-size: 85%;
}
.error {
	float: none;
	color: red;
	font-size: 85%;
}
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Reservation request</title>
	<link rel="stylesheet" href="main.css">
	<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
	<script src="reservation.js"></script>
</head>
	
<body>
	<h1>Reservation Request</h1>
	<form action="response.html" method="get"
    	name="reservation_form" id="reservation_form">
		
		<div id="reservation_tabs">
			<ul>
				<li><a href="#general_information">General Information</a></li>
				<li><a href="#preferences">Preferences</a></li>
				<li><a href="#contact_information">Contact Information</a></li>
			</ul>
			
		
        	
			<div id="general_information">
				<label for="arrival_date">Arrival date:</label>
				<input type="text" name="arrival_date" id="arrival_date" autofocus><br>
				<label for="nights">Nights:</label>
				<input type="text" name="nights" id="nights"><br>
				<label>Adults:</label>
				<select name="adults" id="adults">
					<option value="1">1</option>
					<option value="2">2</option>
					<option value="3">3</option>
					<option value="4">4</option>        		        		        		
				</select><br>
				<label>Children:</label>
				<select name="children" id="children">
					<option value="0">0</option>
					<option value="1">1</option>
					<option value="2">2</option>
					<option value="3">3</option>
					<option value="4">4</option>        		        		        		
				</select><br>   
			</div>
		
		
        	
			<div id="preferences">
				<label>Room type:</label>
				<input type="radio" name="room" id="standard" class="left" checked>Standard&nbsp;&nbsp;&nbsp;	        	
				<input type="radio" name="room" id="business" class="left">Business&nbsp;&nbsp;&nbsp;
				<input type="radio" name="room" id="suite" class="left last">Suite<br>
				<label>Bed type:</label>
				<input type="radio" name="bed" id="king" class="left" checked>King&nbsp;&nbsp;&nbsp;
				<input type="radio" name="bed" id="double" class="left last">Double Double<br>
				<input type="checkbox" name="smoking" id="smoking">Smoking<br>
			</div>
		
    		
			<div id="contact_information">
				<label for="name">Name:</label>
				<input type="text" name="name" id="name"><br>
				<label for="email">Email:</label>
				<input type="text" name="email" id="email"><br>
				<label for="phone">Phone:</label>
				<input type="text" name="phone" id="phone" placeholder="999-999-9999"><br>
			</div>
		</div>
		
    	<fieldset>
    		<input type="button" id="policies" value="View Cancellation Policies">
			<input type="submit" id="submit" value="Submit Request">
			<div id="dialog" title="Cancellation Policies" style="display: none;">
				<p>Notification of cancellation or arrival date change must be 
				received more than three days (72 hours) prior to the confirmed arrival date for the 
				reservation deposit to be refundable. Email notification is acceptable, and a cancellation
				confirmation will be sent to you. Failure to check-in on the scheduled arrival date 
				will result in the cancellation of the reservation including any remaining nights, 
				and the reservation deposit shall be forfeited.</p>
			</div><br>    		
    	</fieldset>

	</form>
</body>
</html>

最佳答案

选项卡是 jQuery UI 的一部分 https://jqueryui.com/tabs/ lib(它与 jQuery 本身是不同的文件,所以你需要它们)你没有引用。 (cdns here )

关于javascript - 选项卡在 jQuery 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43674297/

相关文章:

javascript - 使用 jQuery 手动更改 radio 组时更改事件触发两次

javascript - 如何在 jquery 上的脚本中将 "live"更改为 "on"以获得相同的功能

html - 在固定宽度的 div 中垂直居中无序列表和按钮

javascript - 即使使用 javascript 输入正确的值后,html 表单仍然显示验证错误

javascript - 由于 iframe 的导航,如何更改浏览器中的 URL?

javascript - 代码适用于 JSFiddle,但在本地保存时不起作用

javascript - 尝试使用前端语言(即 html、css 和 javascript)构建骰子游戏

javascript - 确定动态 CSS3 多列 DIV 宽度的宽度 fixed column-width

javascript - 按提交后使用 jquery 将值打印到 div 中?

javascript - 检查控制台是否存在