2011-12-23 80 views
3

我對jQuery相當陌生,我在與代表團有關的問題上掙扎了一下。 在我的代碼我有加載動態頁面:jQuery Mobile .delegate()沒有觸發

$("#results").delegate('li', 'click', function(evt){ 
     var queryData = "id=" + $(this).attr("id"); 
     if ($(this).attr("id")){ 
     $.ajax({ 
      type: "GET", 
      url: "wiw_detail_page.html", 
      data: queryData, 
      beforeSend: onDetailBeforeSend, 
      success: onDetailSuccess, 
      error: onError 
     }); 
     } 
     else 
     { 
     alert("Please select an Employee from the list"); 
     } 
     return false; 
    }); 

這是onDetailSuccess功能:

function onDetailSuccess(data, status){ 
     $("body").append(data); 
     $("#wiw_detail_page").page(); 
     $.mobile.changePage("#wiw_detail_page", { transition: 'slide' }); 
    } 

一切運作良好,因爲我得到我的第一個頁面轉移到「wiw_details_page」。 裏面的新的一頁我有字段列表,下一個可摺疊的,這樣的:

<div data-role="collapsible" data-collapsed="false" data-theme="b" id="comData"> 
    <h2>Communication Data</h2> 
    <div id="communicationData"> 
     <fieldset> 
     <label for="address">Address:</label> 
     <a href="#locationMap"> 
      <input class="goMap" type="text" 
       name="address" id="address" 
       disabled="true" 
       value="<%=employeeprofile-building%>"/> 
     </a> 
     </fieldset> 
    </div> 
    </div> 

我需要附加的地址輸入欄點擊處理程序,因爲這個頁面還沒有在$創建(文件)。就緒()我連接了代表對這樣的:

// Use delegation, since detail view is not availble at document.ready time: 
    $("#wiw_detail_page").delegate('input.goMap','click', function(evt){ 
     if ($(this).attr("id") == 'address'){ 
     alert("Click!"); 
     if (navigator.geolocation) { 
      detectBrowser(); 
      navigator.geolocation.getCurrentPosition(function(position){ 
      newInitialize(position.coords.latitude, position.coords.longitude); 
      }) 
     }else{ 
      detectBrowser(); 
      newInitialize(45.06150, 7.65599); 
     } 
     } 
    }); 

    }); 

這個代表團從未發生,當我點擊輸入字段沒有任何反應(甚至沒有任何類型的錯誤)。

我也將選擇更改爲嘗試:

$("#comData").delegate('input.goMap','click', function(evt) 

而作爲一個絕望的嘗試,我改成了:

$("body").delegate('input.goMap','click', function(evt) 

任何線索?

對不起,提前如此長的問題,謝謝, R.

+0

您使用的是什麼版本的jQuery和jQueryMobile的? – 2011-12-23 02:49:38

+0

jquery-1.7.1.min.js和jquery.mobile-1.0.min.js – RobyPag 2011-12-23 02:55:37

+0

jQM只支持jQuery版本1.6.4,我會建議降級並回發,在這裏更多:http://jquerymobile.com/blog/2011/11/16 /宣佈-jquery-mobile-1-0/ – 2011-12-23 02:59:33

回答

0

你想嘗試#communicationData,如:

$("#communicationData").delegate('input.goMap','click', function(evt) {...}