2012-07-20 114 views
0

我在單個js文件中使用此ajax調用,並將其導入到我正在使用的jsf頁面中。我在Chrome中調試了這個部分,我在$(「#close」)之前設置了一個斷點。然後我在控制檯中鍵入它,它出來的元素的HTML代碼,但它不會單擊該按鈕,請幫助,我的代碼有什麼問題?謝謝。在ajax成功時調用element.click()失敗

function doLogin(username, password) { 
$.ajax({ 
    "type" : "post", 
    "url" : "com.micros.hcp.mobile.ajax.common.AjaxMSMLoginDialog.ajax", 
    data : { 
     "username" : username, 
     "password" : password 
    }, 
    dataType : "text", 
    async: false, 
    error : function(msg) { 
     alert(msg); 
     $.mobile.hidePageLoadingMsg(); 
     notification("Login failed."); 
    }, 
    success : function(data) { 
     data = String(data).trim(); 
     if (data == "login_success") { 
      alert($("#close").attr("id")); // make sure I can get the element 
      $("#close").click(); 
     } else { 
      addErrorMessage(data); 
     } 
    } 
}); 
} 
以下

的結果在Chrome控制檯顯示當我輸入$(「親密」)點擊()。在斷點處。在Ajax調用之後,如果我在控制檯中運行它,它將起作用。

$("#close").click(); 
[ 
<a href=​"#" id=​"close" data-role=​"button" data-rel=​"back" style=​"display:​ none;​ " data-   corners=​"true" data-shadow=​"true" data-iconshadow=​"true" data-wrapperels=​"span" data-theme=​"c" class=​"ui-btn ui-btn-up-c ui-shadow ui-btn-corner-all" data-transition=​"slideup" data- direction=​"reverse">​…​</a>​ 
] 
+0

你做錨標籤上有任何點擊事件?沒有可見的,我可以看到你的代碼... – Chris 2012-07-20 13:06:06

+0

你需要定義它'$(「#close」)。click(function(){alert('clicked');});' – 2012-07-20 13:08:32

+0

是的,抱歉沒有粘貼在這裏。我有這個表單內 – 2012-07-20 13:08:57

回答

-1

定義JS功能:

function clicked(){ 
    alert('clicked'); 
} 

onclick屬性添加到您的<a></a>元素

<a href=​"#" id=​"close" data-role=​"button" data-rel=​"back" style=​"display:​ none;​ " data-   corners=​"true" data-shadow=​"true" data-iconshadow=​"true" data-wrapperels=​"span" data-theme=​"c" class=​"ui-btn ui-btn-up-c ui-shadow ui-btn-corner-all" data-transition=​"slideup" data- direction=​"reverse" onclick="clicked()">​…​</a>​ 

那麼它就會觸發您的自定義功能,這種」

$('#close').click(); 
+0

會是什麼觸發如果OP亙古不變的有綁定到錨標記的任何點擊功能? – 2012-07-20 13:13:26

+2

'$( '#接近')點擊()'IS一樣'$( '#關閉')觸發( '點擊');' – Esailija 2012-07-20 13:13:45

+0

謝謝你的想法,我嘗試過了扳機,但沒有有綁定部分,現在我按照你的說法做了,但它仍然無法工作....當觸發器執行時顯示點擊,但對話框未關閉。該按鈕用於關閉對話框。 – 2012-07-20 13:22:01