【问题标题】:taphold not working with dyanmatically created listview in phonegap Androidtaphold 不适用于 phonegap Android 中动态创建的列表视图
【发布时间】:2013-07-12 11:20:36
【问题描述】:

我有一个列表视图,它影响本地数据库中的数据。我想在列表项上添加和点击事件,所以我使用来自这个站点http://www.raymondcamden.com/index.cfm/2012/5/23/Context-Menu-Example-with-jQuery-Mobile的点击示例但是当在模拟器上运行这个代码时它工作正常但是当我试图将它实现到我正在使用的代码中它不起作用正确地它在 Taphold 事件中给出了一个警报,但没有在这里创建一个示例对话框,这是我正在使用的代码:

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

提前致谢

【问题讨论】:

    标签: jquery-mobile android-listview cordova-2.0.0


    【解决方案1】:

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

    而不是这个:

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

    像这样绑定它:

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

    此解决方案不关心 .namelist 是否存在。点击事件将绑定到文档对象,并且只有在 DOM 中激活时才会传播到 .namelist

    【讨论】:

    • 我试过了,但还是同样的问题,它只显示警告框,点击后不显示对话框。
    • 你能把你的项目发邮件给我,我去看看吗?
    • 对不起,我不能给你邮寄它反对的代码或公司政策,但你能给我一个动态创建列表视图的例子,当用户长按/点击列表项时,我将创建一个弹出菜单来选择它会有很大的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 2021-10-31
    • 2011-02-12
    相关资源
    最近更新 更多