2012-08-14 57 views
-1

我在使用JQuery進行表單驗證時遇到了問題。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> 
+1

你會得到什麼錯誤?爲什麼當前的穩定版本是1.8.0時使用jQuery 1.6.2?第三,當你驗證(http://docs.jquery.com/Plugins/Validation)是一個很好的插件時,你爲什麼要自己進行驗證? – dnagirl 2012-08-14 11:53:02

+1

在傾倒你的頁面之前要做的一件事,使它易於閱讀,使用像http://jsbeautifier.org這樣的工具,消息來源讓我的眼睛受到傷害。 – 2012-08-14 11:53:54

+0

@Mark,不錯的鏈接!我將它添加到我的工具箱 – dnagirl 2012-08-14 12:00:03

回答

0

你有這樣的:

.insertBefore('#contact') 

,但我沒有看到你的頁面上的任何id="contact"元素。有什麼缺失?否則,你需要爲jQuery提供一個有效的選擇器來定位。

另外,你沒有在你的問題中標記這個,但是你的第一行讓我假設你正在做WebForms開發。請注意,任何帶有runat="server"的標記都不會將您輸入標記(或設計者)的id