2013-10-15 13 views
0

我有一段代碼,當我把它放在頁面上時工作正常,當我把腳本放在單獨的文件中時,它沒有工作。當JavaScript把它放在單獨的文件中時,Jquery點擊事件沒有綁定<a>標記

SCRIPT

$(document).ready(function() { 
    $('[id*=SearchLocator]').on("keydown", function (event) { 
     if (event.keyCode == 13) { 
      $('#SearchStoreLocator').trigger("click"); 
     } 
    }); 

    $('#SearchStoreLocator').on("click", function (e) { 
     e.preventDefault(); 
     alert("hello"); 
    }); 
}); 

HTML

<asp:TextBox ID="SearchLocator" runat="server" ValidationGroup="PostCodeSearchValidation" 
ToolTip="Enter an address" MaxLength="200" CssClass="AddressSearch" /> 
<a id="SearchStoreLocator">Find</a>. 

渲染HTML

<input name="ctl00$PageContent$Survey$ctl06$SearchLocator" type="text" maxlength="200" id="ctl00_PageContent_Survey_ctl06_SearchLocator" title="Enter an address" class="AddressSearch" placeholder="Enter a location" autocomplete="off"> 
<a id="SearchStoreLocator">Find</a> 

我不知道怎麼回事就在這裏...只是hello alert時不顯示我的點擊鏈接(當我把腳本放在一個單獨的文件中)

jQuery的版本

jquery-1.8.3.min.js 
jquery-1.9.1.min.js 

both are loaded on the page, BUT other things are working fine except only this <a> tag. 

NOTE: i am loading jquery twice as there are some things that are not supported by jquery-1.8.3 

編輯

after removing jquery-1.8.3.min.js still not working... 
+2

我認爲你缺少DOM準備包裝。準備好DOM腳本。 – PSL

+0

你爲什麼要加載jquery兩次? – Smeegs

+0

爲什麼你使用兩個版本的jQuery和DOM準備丟失? –

回答

0

嘗試使用event.which

if (event.which== 13) { 
     $('#SearchStoreLocator').trigger("click"); 
    } 
1

幾個要點:

  • ASP.Net doesn't使用精確的ID在c lient側(渲染)ClientID MSDN,做所以使用屬性ClientID,我覺得你可以在web.config
  • 只能使用一個jQuery的編輯,我建議jQuery的1.9.1.min.js
  • 是您將在腳本所在的jQuery後腳本文件
+0

好吧,你可以看到ID沒有改變呈現的HTML ...在引用外部JS後,一切正常,除了這標籤 –

+0

在控制檯中沒有錯誤。我不知道如何檢查事件是否附加...請給我一些例子。 –

1

我想你可以用這個選擇它們:我的意思是ID的末端應與「SearchLocator」等於

**$('a [id$=SearchLocator]')** 

**$('[id$=SearchLocator]')** 
相關問題