在jquery移动弹出窗口中动态创建jquery移动列表视图

时间:2013-05-07 20:04:33

标签: jquery jquery-ui jquery-mobile jquery-mobile-listview jquery-mobile-popup

这让我疯了,所以希望有人可以帮助我:-D

我正在尝试创建一个jquery移动弹出窗口,在弹出窗口中我想动态创建一个jQuery移动列表视图。但是,我不断收到以下错误消息。

未捕获的TypeError:无法读取未定义的属性“jQuery19104145257784985006”。

这是我的代码

$('#create').on('click', function () {
    //create a div for the popup
    var $popUp = $("<div/>").popup({
        dismissible: false,
        theme: "a",
        overlyaTheme: "a",
        transition: "pop"
    }).on("popupafterclose", function () {
        //remove the popup when closing
        $(this).remove();
    });

    //create a title for the popup
    $("<ul data-role='listview'/>").trigger("create").appendTo($popUp);

    $popUp.popup('open').trigger("create");
});

我在这里创建了一个基本的jsFiddle http://jsfiddle.net/QA7Dm/

感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

工作示例:http://jsfiddle.net/Gajotres/Ar8N3/

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>    
    </head>
    <body>
        <div data-role="page" id="index">
            <div data-theme="a" data-role="header">
                <h3>
                    First Page
                </h3>
                <a href="#second" class="ui-btn-right">Next</a>
            </div>

            <div data-role="content">
                <a href="#" data-role="button" id="create">Create a popup</a>
            </div>

            <div data-theme="a" data-role="footer" data-position="fixed">

            </div>
        </div>     
    </body>
</html>   

JS:

$(document).on('pageshow', '#index', function(){ 
    $(document).on('click', '#create', function(){     
        $('<div>').attr({'data-role':'popup','id':'popupBasic','data-dismissible':'false','data-theme':'a','data-transition':'pop'}).appendTo('[data-role="content"]');
        $('<div>').attr({'data-role':'header','data-theme':'b','id':'popup-header'}).append('<h1>Header</h1>').appendTo('#popupBasic');
        $('<ul>').attr({'data-role':'listview','id':'list-test','data-theme':'a'}).appendTo('#popupBasic');
        $('<li>').append('List test').appendTo('#list-test');
        $('#index').trigger('pagecreate');
        var popup = setInterval(function(){
            $("#popupBasic").popup("open",{
                overlyaTheme: "a"
            }).on("popupafterclose", function () {
                //remove the popup when closing
                $(this).remove();
            });
            clearInterval(popup);
        },1);      
    });    
});

答案 1 :(得分:1)

这是修复。

$('#create').on('click', function () {
var $popUp = $("<div/>").popup({
    dismissible: false,
    theme: "a",
    overlyaTheme: "a",
    transition: "pop"
 }).on("popupafterclose", function () {
    $(this).remove();
 });
 $("<ul id='test' data-role='listview'/>").trigger("refresh").appendTo($popUp);
$popUp.popup('open');
});

演示:http://jsfiddle.net/Palestinian/QA7Dm/2/