javascript - ajax 调用上的 jQuery 加载器

标签 javascript php jquery html ajax

我现在可以让加载器也使用代码 - 但它不会替换并调用 URL。所以 ajax url 调用应该在可搜索中调用:

<button onclick="myFunction()">LOAD</button><br /><br />
<div class="spinner bar hide">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="searchtable"><?php include 'hotels/hotelList.php';?></div>
<script>
function myFunction() { 
$('.searchtable').addClass('hide');
$('.spinner').removeClass('hide');

$.ajax({
url: 'hotels/hotelSortBy.php?name=<?php echo $name;?>&arrival=<?php echo $arrival;?>&departure=<?php echo $departure;?>&guests=<?php echo $numberOfGuests;?>'
})

.done(function() {
$('.spinner').addClass('hide');
$('.searchtable').removeClass('hide');
});

}
 </script> 

最佳答案

我会提出一些建议:

  • 不要使用 id,使用类。

  • 不要使用 .hide.show Paul Irish 有一个很好的解释 here.

  • 从 jq 1.7 开始,您真的不应该使用 .bind()

  • 我建议使用 .ajax() 代替 .load() see docs

那么你的函数看起来就像这样

function myFunction() {

  $('.search-table').addClass('hide');
  $('.spinner').removeClass('hide');

  $.ajax({
    url: 'path/to/endpoint'
  })
  .done(function() {
    $('.spinner').addClass('hide');
    $('.search-table').removeClass('hide');
  });

}

工作 jsbin 示例 here.

关于javascript - ajax 调用上的 jQuery 加载器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28596104/

相关文章:

php - Symfony 应用程序的运行时配置

php - 防止在 ajax 请求上执行整个 php

php - 提交 PHP 电子邮件表单后如何重定向到 HTML 页面?

javascript - ajax 成功后,更改另一个链接值

javascript - 为什么我无法将 Java 字符串值从 zscript 传递到 ZK 中的 javascript?

javascript - 如何让div在按钮点击时淡入下面最近的div?

javascript - 使用javascript读取PDF文件

JavaScript:为什么 FileReader.onload 在 mozilla 中没有被调用?

javascript - 使用 Angular.js 和 jQuery 转换时隐藏的元素

javascript - 第一次点击时提交按钮不起作用