我在使用 JQuery 进行表单验证时遇到问题。
我的代码应该在页面加载时在页面顶部显示一个解释必填字段的段落。这一段没有出现。
这是我的完整文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="form_jquery.aspx.cs"
Inherits="form_jquery" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="Stylesheet" href="form.css" type="text/css" media="screen"
/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function () {
var requiredFlag = ' * ';
var requiredKey = $('input.required:first').next('span').text();
requiredKey = requiredFlag + requiredKey.replace(/^\((.+)\)$/, "$1");
var conditionalFlag = ' ** ';
var conditionalKey = $('input.conditional:first').next('span').text();
conditionalKey = conditionalFlag + conditionalKey.replace(/\((.+)\)/, "$1");
$('form :input').filter('.required').next('span').text(requiredFlag).end().prev('label').addClass('req-label');
$('form :input').filter('.conditional').next('span').text(conditionalFlag);
$('<p></p>').addClass('field-keys').append(requiredKey + '<br />').append(conditionalKey).insertBefore('#contact');
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset>
<legend>Personal Info</legend>
<ol>
<li>
<label for="first-name">First Name</label>
<input class="required" type="text" name="first-name"
id="first-name" />
<span>(required)</span>
</li>
<li>
<label for="last-name">Last Name</label>
<input class="required" type="text" name="last-name"
id="last-name" />
<span>(required)</span>
</li>
<li>How would you like to be contacted? (choose at least one method)
<ul>
<li>
<label for="by-email">
<input type="checkbox" name="by-contact-type" value="E-mail" id="by-email"
/>by E-Mail</label>
<input class="conditional" type="text" name="email" id="email"
/>
<span>(required when corresponding checkbox checked)</span>
</li>
<li>
<label for="by-phone">
<input type="checkbox" name="by- contact-type" value="Phone" id="by-phone"
/>by Phone</label>
<input class="conditional" type="text" name="phone" id="phone"
/>
<span>(required when corresponding checkbox checked)</span>
</li>
<li>
<label for="by-fax">
<input type="checkbox" name="by- contact-type" value="Fax" id="by-fax"
/>by Fax</label>
<input class="conditional" type="text" name="fax" id="fax"
/>
<span>(required when corresponding checkbox checked)</span>
</li>
</ul>
</li>
</ol>
</fieldset>
</div>
</form>
</body>
</html>