首页 文章

taphold无法使用phonegap Android中的动态创建列表视图

提问于
浏览
0

我有一个listview,它从本地数据库中获取数据 . 我想在列表项目上添加和taphold事件,所以我使用此网站http://www.raymondcamden.com/index.cfm/2012/5/23/Context-Menu-Example-with-jQuery-Mobile的taphold示例但是当在模拟器上运行此代码时它的工作正常,但是当我尝试将其实现到代码中时,我在这里创建了一个samplesialog框,我正在使用的代码:

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

提前致谢

1 回答

  • 1

    使用动态创建的内容和jQuery Mobile时,您必须使用委托事件绑定 .

    而不是这个:

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

    像这样绑定它:

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

    此解决方案不关心 .namelist 是否存在 . Tap事件将绑定到文档对象,只有当它在DOM中变为活动状态时才会传播到 .namelist .

相关问题