加载外部HTML,其中包含一个ajax调用servlet脚本,并只执行一次

jQuery Mobile Load external HTML that contains an ajax call to servlet script and execute them only once

本文关键字:执行 一次 脚本 调用 HTML 外部 包含一 加载 ajax servlet      更新时间:2023-09-26

我有一个iphone风格的菜单为web应用程序POS,当我按下客户端按钮,它显示了一个页面与所有的客户端,这是使ajax调用java servlet,我使用load()函数在主页带来的GetCustomers.html文件与一个脚本包含jquery代码与ajax和UL标签,当我在我的主页中调用它(加载它)时,它只格式化UL及其分别生成的LI,当我使用firebug调试器在触发器('create')中带有断点时;这是"pagebeforeshow"函数内的最后一条指令。但是!如果我在没有调试的情况下运行它,它会格式化和样式化它,并显示它2毫秒,然后它显示它没有jquery-mobile Listview样式。我使用$(this).trigger('create');和that.listview("更新");但它仍然不能很好地工作……有什么建议吗?

这是主页代码:

$(document).on("pagebeforeshow", "#pagewelcomeclientes", function(){
  if( navigationInitialized == 'X'){
    var newItems = '<li data-role="list-divider">Page Options</li>';
    newItems += '<li><a href="#" class="time">Time</a></li>';
    newItems += '<li><a href="#pageclientenuevo" id="botonclientenuevo" data-icon="plus" class="clientenuevo" data-transition="fade">Agregar Cliente</a></li>';
    $(".pageOpts").empty();
    $(".pageOpts").append(newItems).listview("refresh");
    $.ajaxSetup ({  
        cache: false  
    });  
    var loadUrl = "GetCustomers.html";
    $("#contentwelcomeclientes").load(loadUrl,function(){
    BringClientes();
    $(this).trigger('create');

    });
    var list = $('ul[data-autodividers="true"][data-sort="true"]');
    list.each(function (i, item) {
      var that = $(item);
        // Gel all the list items into an array (excluding te dividers)
        var lis = that.find('li').not('.ui-li-divider').get();
        // Sort the list items alphabetically, descending
        lis.sort(function (a, b) {
            var valA = $(a).text(),
                valB = $(b).text();
            if (valA < valB) {
                return -1;
            }
            if (valA > valB) {
                return 1;
            }
            return 0;
        });
        // Remove all list items
        that.empty();
        // Rebuild the list with the ordered items
        $.each(lis, function (i, li) {
            that.append(li);
        });
        // Refresh/rebuild the listview
       that.listview('refresh');
    }); 
  $(this).trigger('create');
  }
});

这里调用HTML:

    <script type='text/javascript'>
        BringClientes = function() {
            $.ajax({
            type: "POST",
            url: "GetCustomers",
            dataType: "json",
            success: function (data) {
                if (data.Customers.length) {
                    $.each(data.Customers, function (i, data) {
                        var temp = null;    
                        var temp = "#cd-dropdown";
                        var temp = temp+data.msg_id;
                        var msg_data = 
                        '<li><a href="#">'+data.first_name+'</a><a href="#paginaclientesolo2"></a></li>';
                        $(msg_data).appendTo("#juntar");
                    });
                } else {
                    $("#content").html("No Results");
                }
            }
        });

    }
    </script>
  <ul   data-role="listview" 
        data-inset="true" 
        data-filter="true"
        data-autodividers="true" 
        data-sort="true" 
        data-theme="c"
        data-divider-theme="c"
        data-split-icon="grid" 
        data-split-theme="d"
        id="juntar"
        >
   </ul>

我认为它在第二页执行两次脚本,我试图做的是修复它,是命名函数,然后只调用一次,但它仍然不起作用…如果你需要样本图片,我可以通过邮件发送,这是我的第一个问题,stack不允许我上传图片…

提前感谢!!div =)

让我们将下面的$(document).on("pagebeforeshow", "#pagewelcomeclientes", function(){});行更改为如下

$('#pagewelcomeclientes').on('pagebeforeshow',function(event){
   // your code goes here
});

同样将$(this).trigger('create');改为$('#pagewelcomeclientes').trigger('create');,看看会发生什么

如果你怀疑服务被调用了两次,那么更好的解决方法是使用.html()而不是使用.append()

你可以修改你的脚本如下

$.ajax({
        type: "POST",
        url: "GetCustomers",
        dataType: "json",
        success: function (data) {
            if (data.Customers.length) {
            var myListstring = "";
                $.each(data.Customers, function (i, data) {
                    var temp = null;    
                    var temp = "#cd-dropdown";
                    var temp = temp+data.msg_id;
                          myListstring += '<li><a href="#">'+data.first_name+'</a><a href="#paginaclientesolo2"></a></li>';
                  });
                  $("#juntar").html(myListstring);
                  $("#juntar").listview();
            } else {
                $("#content").html("No Results");
            }
        }
});

我建议你不要使用.load()来使事情复杂化,你可以在页面本身调用服务并呈现listview。这将产生可读性更强的代码。