【问题标题】:JQM 1.4.2 Adding template from ajax to popup not renderingJQM 1.4.2 将模板从 ajax 添加到弹出窗口不呈现
【发布时间】:2014-05-17 16:08:58
【问题描述】:

我通过 .get 从 php 文件中获取模板,但无法正确呈现输入或列表视图。

当我直接在 js 文件中附加 html 时,它会正确呈现,所以我认为它与如何返回模板中的数据有关。

提前致谢。

js

$(document).ready(function() {
$('#addOffice, #addDoctor').on('click', function() {

    var $popUp = $("<div/>").popup({
    dismissible : false,
    theme : "b",
    overlayTheme : "b",
    transition : "pop"
    }).on("popupafterclose", function() {
        $(this).remove();
    }).css({
        'width': '400px',
        'padding': '10px'
    });     
    $.get('../templates/'+$(this).attr('id')+'.php', function(data){
        $(data).appendTo($popUp);
    });
    $popUp.popup('open').trigger("create");
});
});

php模板

<form id="doctorAdd">
    <div class="ui-field-contain">
        <label for="doctorName">Full Name<span style="color:rgba(191,191,191,1.00); font-size:.8em; float:right;"> ( Required )</span></label>
        <input type="text" name="doctorName" id="doctorName" placeholder="Full Name" value="">
    </div>    
    <div class="ui-field-contain">
        <label for="doctorDOB">DOB<span style="color:rgba(191,191,191,1.00); font-size:.8em; float:right;"> ( Required )</span></label>
        <input type="text" name="doctorDOB" id="doctorDOB" placeholder="DOB" value="">
    </div>    

    <ul data-role="listview" data-inset="true" style="margin:20px 0px 0px 0px;">
      <li><a href="#" style="text-align:center;" data-rel="back" class="ui-btn ui-mini">Add</a></li>
      <li><a href="#" style="text-align:center;" data-rel="back" class="ui-btn ui-mini">Clear</a></li>
    </ul> 
</form>

【问题讨论】:

    标签: javascript jquery ajax jquery-mobile


    【解决方案1】:

    工作示例:http://jsfiddle.net/Gajotres/45V7G/

    JavaScript:

    $(document).on('pageshow', '#index', function(){ 
        $(document).on('click', '#addPopup',function() {
            // close button
            var closeBtn = $('<a href="#" data-rel="back" data-role="button" data-theme="b" data-icon="delete" data-iconpos="notext" class="ui-btn-right">Close</a>').button();
            
            // text you get from Ajax
            var content = '<form id="doctorAdd"><div class="ui-field-contain"><label for="doctorName">Full Name<span style="color:rgba(191,191,191,1.00); font-size:.8em; float:right;"> ( Required )</span></label><input type="text" name="doctorName" id="doctorName" placeholder="Full Name" value=""></div>    <div class="ui-field-contain"><label for="doctorDOB">DOB<span style="color:rgba(191,191,191,1.00); font-size:.8em; float:right;"> ( Required )</span></label><input type="text" name="doctorDOB" id="doctorDOB" placeholder="DOB" value=""></div>    <ul data-role="listview" data-inset="true" style="margin:20px 0px 0px 0px;"><li><a href="#" style="text-align:center;" data-rel="back" class="ui-btn ui-mini">Add</a></li><li><a href="#" style="text-align:center;" data-rel="back" class="ui-btn ui-mini">Clear</a></li></ul> </form>';
            
            // Popup body - set width is optional - append button and Ajax msg
            var popup = $("<div/>", {
                "data-role": "popup",
                "class" : "ui-content"
            }).css({
                "width": "400px"
            }).append(closeBtn).append(content);
            
            // Append it to active page
            $(".ui-page-active").append(popup);
            
            // Create it and add listener to delete it once it's closed
            // open it
            $("[data-role=popup]").on("popupafterclose", function () {
                $(this).remove();
            }).on("popupafteropen", function () {
                $(this).popup("reposition", {
                    "positionTo": "window"
                });
            }).popup({
                dismissible : false,
                theme : "b",
                overlayTheme : "b",
                transition : "pop"
            }).enhanceWithin().popup("open");           
        }); 
    });
    
    • 使用 .enhanceWithin() 而不是 trigger('create')
    • trigger('create') 已弃用,仅在页面内容 div 上正确使用

    最后一点,这里使用的代码不会这样工作,因为你使用的是$.get,因为它是异步过程,基本上你需要使用回调函数来增强你的代码。

    基本上你的代码应该是这样的:

    $.get( '../templates/'+$(this).attr('id')+'.php').done(function( data ) {
        // close button
        var closeBtn = $('<a href="#" data-rel="back" data-role="button" data-theme="b" data-icon="delete" data-iconpos="notext" class="ui-btn-right">Close</a>').button();
        
        // text you get from Ajax
        //var content = '<form id="doctorAdd"><div class="ui-field-contain"><label for="doctorName">Full Name<span style="color:rgba(191,191,191,1.00); font-size:.8em; float:right;"> ( Required )</span></label><input type="text" name="doctorName" id="doctorName" placeholder="Full Name" value=""></div>    <div class="ui-field-contain"><label for="doctorDOB">DOB<span style="color:rgba(191,191,191,1.00); font-size:.8em; float:right;"> ( Required )</span></label><input type="text" name="doctorDOB" id="doctorDOB" placeholder="DOB" value=""></div>    <ul data-role="listview" data-inset="true" style="margin:20px 0px 0px 0px;"><li><a href="#" style="text-align:center;" data-rel="back" class="ui-btn ui-mini">Add</a></li><li><a href="#" style="text-align:center;" data-rel="back" class="ui-btn ui-mini">Clear</a></li></ul> </form>';
        
        // Popup body - set width is optional - append button and Ajax msg
        var popup = $("<div/>", {
            "data-role": "popup",
            "class" : "ui-content"
        }).css({
            "width": "400px"
        }).append(closeBtn).append(data); // Here we are using data response from $.get
        
        // Append it to active page
        $(".ui-page-active").append(popup);
        
        // Create it and add listener to delete it once it's closed
        // open it
        $("[data-role=popup]").on("popupafterclose", function () {
            $(this).remove();
        }).on("popupafteropen", function () {
            $(this).popup("reposition", {
                "positionTo": "window"
            });
        }).popup({
            dismissible : false,
            theme : "b",
            overlayTheme : "b",
            transition : "pop"
        }).enhanceWithin().popup("open");           
    });
    

    【讨论】:

    • 感谢 Gajotres!!!我很高兴是你回答,总是喜欢看到你的帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 2015-03-09
    相关资源
    最近更新 更多