加载外部HTML,其中包含一个ajax调用servlet脚本,并只执行一次
jQuery Mobile Load external HTML that contains an ajax call to servlet script and execute them only once
我有一个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。这将产生可读性更强的代码。
- If语句只执行一次
- JavaScript 只在 php 代码中执行一次
- 就绪函数上的指令只能执行一次
- 如何在if循环中只执行一次
- Javascript执行一次
- Jquery on('单击')..函数只执行一次
- 如何只执行一次ajaxStart和ajaxEnd
- 使用Greasemonkey每5秒执行一次Javascript
- 只执行一次jquery animate
- jQuery如果大于则执行操作,但只执行一次
- 制作一个$window.location.reload(true);在离子/角度中仅执行一次
- 正则表达式回调函数只执行一次
- 代码只执行一次,但应该继续执行
- Casper.start 的循环只执行一次
- 如何在一次点击(而不是整个点击)内执行一次操作
- 全局 JS 文件:仅执行一次 JSON 调用
- 函数仅执行一次
- 反应:仅在单击 html 元素时执行一次操作
- 浏览器化只执行一次模块代码,在多个需要调用上
- 如何使函数在 2 分钟后执行,然后每隔 2 分钟执行一次