2013-01-11 50 views
0

我有一些jQuery代碼,執行功能doneTyping用戶完成打字時,這與任何代碼工作。但是,當我嘗試使用「$('#searchf')。submit();」表單不提交。我嘗試了很多變化。表單不提交裏面的功能jquery

有誰知道它爲什麼不工作? - jQuery代碼如下

<script> 
$(document).ready(function() { 
    $("#cat").change(function() { 
    this.form.submit(); 
}) 

var typingTimer;    //timer identifier 
var doneTypingInterval = 1000; //time in ms, 5 second for example 

//on keyup, start the countdown 
$('#legal').keyup(function(){ 
    clearTimeout(typingTimer); 
    if ($('#legal').val) { 
     typingTimer = setTimeout(doneTyping, doneTypingInterval); 
    } 
}); 

//user is "finished typing," do something 
function doneTyping() { 
    $('#searchf').submit(); 
    alert("Your book is overdue."); 
} 

}); 
</script> 

頁面代碼

<%@ LANGUAGE="VBSCRIPT" %> 
<% 
pagetitle="Contractor Details" 
%> 
<!--#INCLUDE VIRTUAL="/_lib/include/header.htm"--> 
<!--#INCLUDE VIRTUAL="/_lib/include/menu.htm"--> 
<script> 
$(document).ready(function() { 
    $("#cat").change(function() { 
    this.form.submit(); 
}) 

var typingTimer;    //timer identifier 
var doneTypingInterval = 1000; //time in ms, 5 second for example 

//on keyup, start the countdown 
$('#legal').keyup(function(){ 
    clearTimeout(typingTimer); 
    if ($('#legal').val) { 
     typingTimer = setTimeout(doneTyping, doneTypingInterval); 
    } 
}); 

//user is "finished typing," do something 
function doneTyping() { 
    $('#searchf').submit(); 
    alert("Your book is overdue."); 
} 

}); 
</script> 
<div class="twoColumnRow"> 
<div class="twoColumnContent"> 
<p class="breadCrm"><a href="/index.htm">Home</a> <span>&gt;</span> <a href="/tools/index.htm">Tools of the Trade</a> <span>&gt;</span> <a href="/tools/finance/index.htm">Finance and Purchasing</a><span>&gt;</span> <a href="/tools/finance/procurement/index.htm">Procurement and Contracts information</a></p> 
<div class="contentPad"> 
<!-- Start of main content --> 

<p class="imageRight">&nbsp;</p> 
<h1><%=pagetitle%></h1> 

<% 
     Dim connectString, connect, conDB, sconDB, lDB, con, scon, lcon, src_abn, src_cat 
     connectString = "Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=" & Server.MapPath("data") 
       src_legal = Request.QueryString("legal") 
       src_cat = Request.QueryString("cat") 
     set connect = Server.CreateObject("ADODB.connection") 
     connect.open connectString 

     if src_cat = "all" then 
     conDB = "SELECT * FROM cont.csv WHERE ucase(legal) LIKE ucase('%"+src_legal+"%')" 
     lDB = "SELECT * FROM cont.csv WHERE ucase(legal) LIKE ucase('%"+src_legal+"%')" 
     else 
     conDB = " SELECT * FROM cont.csv WHERE " & src_cat & " = 'Yes' AND ucase(legal) LIKE ucase('%"+src_legal+"%')" 
     lDB = " SELECT * FROM cont.csv WHERE " & src_cat & " = 'Yes' AND ucase(legal) LIKE ucase('%"+src_legal+"%')" 
     end if 

     sconDB = "SELECT * FROM cont.csv" 

     set con = connect.execute(conDB) 
     set scon = connect.execute(sconDB) 
     set lcon = connect.execute(lDB) 
%> 
<h2>Search results</h2> 
<% if not con.BOF then %> 
<div style="margin-top:1em"> 

    <form i="searchf" action="dbinstant.htm" method="get"> 

     <p>Company name:1<br/> 
     <input id="legal" name="legal" type="text" value="<%=src_legal%>"> 
     </p> 
    <p>Categories for Creative Design:<br/> 
     <select id="cat" name="cat"> 
     <option class="group" label="All categories" value="all" <%if request.querystring("cat") = "all" then response.write("selected") %>></option> 
     <option class="group" label="Strategic brand, marketing and communications advice" value="a" <%if request.querystring("cat") = "a" then response.write("selected") %>></option> 
     <option class="group" label="Graphic design and layout" value="b" <%if request.querystring("cat") = "b" then response.write("selected") %>></option> 
     <option class="group" label="Forms design" value="c" <%if request.querystring("cat") = "c" then response.write("selected") %>></option> 
     <option class="group" label="Web design and development" value="d" <%if request.querystring("cat") = "d" then response.write("selected") %>></option> 
     <option class="group" label="Authoring services" value="e" <%if request.querystring("cat") = "e" then response.write("selected") %>></option> 
     <option class="group" label="Editorial services" value="f" <%if request.querystring("cat") = "f" then response.write("selected") %>></option> 
     <option class="group" label="Translation services" value="g" <%if request.querystring("cat") = "g" then response.write("selected") %>></option> 
     <option class="group" label="Photography and film services" value="h" <%if request.querystring("cat") = "h" then response.write("selected") %>></option> 
     <option class="group" label="Scanning and digitisation services" value="i" <%if request.querystring("cat") = "i" then response.write("selected") %>></option> 
     <option class="group" label="Multimedia editing services" value="j" <%if request.querystring("cat") = "j" then response.write("selected") %>></option> 
     </select> 
    </p> 
    <input type="submit" value="Search" /> 
    </form> 
    <br /> 

    <table style="font-size:.9em;" class="contentTable"> 
      <tr> 
       <th>ABN:</th> 
       <th>Company Name:</td> 
      </tr> 
    <% con.Movefirst() %> 
    <% while (NOT con.EOF) %> 
      <tr> 
       <td width="120px"><%=con("abn")%></th> 
       <td><a href="dbcomp.htm?abn=<%=con("abn")%>&cat=all"><%=con("legal")%></a></td> 
      </tr> 
    <% 
    con.MoveNext() 
    Wend 
    %> 
</table> 

</div> 
<% else %> 
    <% scon.Movefirst() %> 
    <form action="dbinstant.htm" method="get"> 

     <p>Company name:2<br/> 
     <input id="legal" name="legal" type="text"> 
     </p> 

    <p>Categories for Creative Design:<br/> 
     <select id="cat" name="cat"> 
     <option class="group" label="All categories" value="all" <%if request.querystring("cat") = "all" then response.write("selected") %>></option> 
     <option class="group" label="Strategic brand, marketing and communications advice" value="a" <%if request.querystring("cat") = "a" then response.write("selected") %>></option> 
     <option class="group" label="Graphic design and layout" value="b" <%if request.querystring("cat") = "b" then response.write("selected") %>></option> 
     <option class="group" label="Forms design" value="c" <%if request.querystring("cat") = "c" then response.write("selected") %>></option> 
     <option class="group" label="Web design and development" value="d" <%if request.querystring("cat") = "d" then response.write("selected") %>></option> 
     <option class="group" label="Authoring services" value="e" <%if request.querystring("cat") = "e" then response.write("selected") %>></option> 
     <option class="group" label="Editorial services" value="f" <%if request.querystring("cat") = "f" then response.write("selected") %>></option> 
     <option class="group" label="Translation services" value="g" <%if request.querystring("cat") = "g" then response.write("selected") %>></option> 
     <option class="group" label="Photography and film services" value="h" <%if request.querystring("cat") = "h" then response.write("selected") %>></option> 
     <option class="group" label="Scanning and digitisation services" value="i" <%if request.querystring("cat") = "i" then response.write("selected") %>></option> 
     <option class="group" label="Multimedia editing services" value="j" <%if request.querystring("cat") = "j" then response.write("selected") %>></option> 
     </select> 
    </p> 
    <input type="submit" value="Search" /> 
    </form> 
    <br /><hr /> 
<p>No records match your query.</p> 
<p><a href="dbtest.htm">Return to search page</a></p> 
<% 
    end if 
    con.close 
%> 


<!-- End of main content --> 
</div> <!-- end contentPad div --> 
</div> <!-- end twocolumncontent div --> 
<div class="twoColumnLinks"> 

<!--<div class="relatedLinks"> 
<h3>Related Links</h3> 
<ul> 
<li><a href="/index.htm">Related link 1</a></li> 
</ul> 
</div>--> <!-- end relatedlinks div --> 
<!--#INCLUDE VIRTUAL="/_lib/include/quicklinks.htm"--> 
<!--#INCLUDE VIRTUAL="/_lib/include/mylinks.htm"--> 
</div> <!-- end twocolumnlinks div --> 
</div> <!-- end twocolumnrow div --> 
<!--#INCLUDE VIRTUAL="/_lib/include/footer.htm"--> 
+0

'<形式I = 「searchf」'...錯字? –

回答

2

this.form總是會在你的背景下undefined因爲你從來沒有設置。

this.form 
    ↑ ↑ 
    | └── 'form' is seen as a property of the 'window' object 
    └── this is the 'window' object 

,你可能會想「說」什麼是

$("#cat").change(function() { 
    $(this).form.submit(); 
}); 

但即使這樣,也導致not a property異常作爲form不是屬性或$("#cat")對象...

所以,爲了讓你更容易,而不是this.form.submit();將其更改爲:

$("form").submit(); 

或具體(因爲當你必須在頁面不止一種形式)

<form id="form1" ... > 

$("#form1").submit(); 

即使你沒有在頁面超過一個<form>,它總是使用一個很好的做法第二種方法!

0

我想你只是輸入了一個表單標籤的id。

在您的代碼:

<form i="searchf" action="dbinstant.htm" method="get"> 

正確:

<form id="searchf" action="dbinstant.htm" method="get"> 
+0

* -1 *這本身並不能解決問題! **所有'id'的第一個**不是必需的,'i'是一個有效的屬性,因爲我們可以寫任何我們想要的東西......瀏覽器會忽略它(與data-'屬性的原理相同); **第二**即使在您將「我」更改爲「ID」後,由於我在我的回答中公開的原因,該表格仍不會提交。 – balexandre

+0

雖然並非所有的id都是必需的,但如果id沒有被添加到表單中,那麼doneTyping函數中的$('#searchf')。submit()不起作用。 – tenifre