-1

我在使用 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>
4

1 回答 1

0

你有这个:

.insertBefore('#contact')

id="contact"但我在您的页面上没有看到任何带有 a 的元素。有什么遗漏吗?否则,您需要为 jQuery 提供一个有效的选择器来定位。

另外,你没有在你的问题中标记这个,但是你的第一行让我假设你正在做 WebForms 开发。请注意,任何带有 a 的标记runat="server"都不id会让您在标记(或设计器)中键入。

于 2012-08-15T06:32:11.133 回答