我在使用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>
答案 0 :(得分:0)
你有这个:
.insertBefore('#contact')
但我在您的网页上看不到任何带有id="contact"
的元素。缺少什么?否则,您需要为jQuery提供一个有效的选择器来定位。
另外,你没有在你的问题中标记这一点,但是你的顶线让我假设你正在进行WebForms开发。请注意,任何带有runat="server"
的标记都不会在您的标记(或设计人员)中输入id
。