【问题标题】:Can't ajax reload without page reloading没有页面重新加载就无法重新加载ajax
【发布时间】:2017-03-27 20:32:12
【问题描述】:

我希望它只“重新加载”到#searchuserc。查了很多资料,还是没能解决问题。

 $(document).ready(function() {
    $("#onlineusers").load("online.php");
    $("#searchuserc").show();
    setInterval(function() {
      $("#onlineusers").load("online.php");
    }, 5000);
    $(".search").click(function() {
      $("#onlineusers").hide();
      e.preventDefault(); // Prevent Default Submission
      $.ajax({
          url: 'searchuser.php',
          type: 'POST',
          data: $(this).serialize(), // it will serialize the form data
          dataType: 'html'
        })
        .done(function(data) {
          $('#searchuserc').fadeOut('slow', function() {
            $('#searchuserc').fadeIn('slow').html(data);
          });
        })
        .fail(function() {
          alert('Ajax Submit Failed ...');
        });
    });
  });

这是我的 HTML 文件

 <form method="POST">
  <input type="text" id="searchuser" name="searchuser" placeholder="Sök efter en användare..." />
  <input type="submit" class="search" name="searchbtn" value="Sök" />
</form>
<div id="searchuserc">
</div>

【问题讨论】:

  • 你调用了e.preventDefault()但没有定义e,检查你的js控制台是否有错误,我认为你需要$(".search").click(function(e){跨度>
  • $(this).serialize() - 点击的按钮?
  • 也许把$('#searchuserc').fadeOut('slow', function() { $('#searchuserc').fadeIn('slow').html(data); });改成$('#searchuserc').fadeOut('slow', function() { $(this).html(data); }).fadeIn('slow');

标签: javascript jquery ajax


【解决方案1】:

代替

$(".search").click(function() {
  ...

使用

$(".search").closest("form").submit(function(e) {
  ...

处理程序中的代码可以保持不变。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2019-09-13
    • 2012-02-22
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多