2013-07-12 19 views
0

我有一個列表視圖,它可以從本地數據庫中獲取數據。我想添加和taphold事件的列表項目,所以我使用taphold從這個網站http://www.raymondcamden.com/index.cfm/2012/5/23/Context-Menu-Example-with-jQuery-Mobile例如,但是當在模擬器上運行此代碼時它的工作正常,但是當我試圖將其實施到代碼中,我正在使用它不能正常工作給予警告那它在Taphold事件,但不會在這裏營造出sampledialog箱,我現在用的是代碼:使用taphold不能在phonegap中用danymatically創建listview Android

function createlist(){ 
alert("I am in create list"); 
db.transaction(function(tx){ 
tx.executeSql(select_nameUserDetails,[],function(tx,results){ 
    $('#name').empty(); 
    alert("from list "+results.rows.length); 
    if(results.rows.length>0) 
     { 
      for(var i=0;i<results.rows.length;i++) 
       { 
       alert(i); 
       $('#name').append('<li><p class="namelist" id="s'+i+'" onclick="selectname('+results.rows.item(i).id+')">'+results.rows.item(i).frist_name+'</p></li'); 
       temp_id=results.rows.item(i).id; 
       dataobj[temp_id]=results.rows.item(i).frist_name; 
       alert(dataobj[temp_id]); 
       } 


     else{ 
     alert("No data present"); 
     } 
    }); 
$(document).on("taphold",".namelist",function(event){ 
    alert("I am in taphold event"); 
    event.stopPropagation(); 
    $(this).simpledialog2({ 
    mode:"blank", 
    headerText:"Image Options", 
    showModal:false, 
    forceInput:true, 
    headerClose:true, 
    blankContent:"<ul data-role='listview'><li><a href=''>Edit</a></li><li><a href=''>Delete</a></li></ul>" 
    }); 
}); 
}); 
} 

這裏是js和css文件:

<link rel="stylesheet" href="contactcss/jquery.mobile-1.3.1.min.css"> 
<link rel="stylesheet" href="css/jquery.mobile.simpledialog.min.css"> 
<script src="js/jquery.js"></script><script src="js/index.js"></script> 
<script src="js/jquery.mobile-1.3.1.min.js"></script> 
<script src="js/jquery.mobile.simpledialog2.min.js"></script> 
<script type="text/javascript" charset="utf-8" src="js/cordova-2.7.0.js"></script> 

這裏該html代碼:

<div data-role="Content" > 
<div data-role="listview" id="field"> 
    <ul class="Name" id="name" data-role="listview" data-inset="true" data-theme="b"> 

    </ul> 
</div> 

logcat的錯誤我得到:

07-13 17:29:33.145: D/CordovaLog(2696): TypeError: Result of expression 'o[0]' [undefined] is not an object. 
07-13 17:29:33.145: E/Web Console(2696): TypeError: Result of expression 'o[0]' [undefined] is not an object. at file:///android_asset/www/js/jquery.mobile-1.3.1.min.js:4 

在此先感謝

+0

任何控制檯錯誤? – krishgopinath

回答

1

當與動態創建的內容和jQuery Mobile的工作,你必須使用委託的事件綁定。

取而代之的是:

$(".namelist").on("taphold",function(event){ 

綁定這樣的:

$(document).on("taphold",".namelist",function(event){ 

此解決方案,如果.namelist存在與否不在乎。點擊事件將被綁定到文檔對象,並且僅當它在DOM中變爲活動狀態時纔會傳播到.namelist

+0

我試過這個,但仍然是同樣的問題,它只顯示警告框,點擊後它不顯示對話框。 –

+0

你可以寄給我你的項目,我會看看嗎? – Gajotres

+0

對不起,我不能寄給你的代碼it againts或公司的政策,但你可以給我一個例子,其中listview是動態創建的,當用戶longpress/taphold列表項時,我將創建一個popupmenu來選擇它將是一個很大的幫助。 –