【问题标题】:Ajax Loader on button click - error with code按钮单击时的 Ajax 加载器 - 代码错误
【发布时间】:2017-12-07 13:41:00
【问题描述】:

我遵循了其他关于在单击按钮以提交表单时显示 Ajax 加载 Gif 的建议,但是我似乎无法使代码正常工作。

非常感谢任何帮助。

    <p>
        <input type="hidden" name="job_manager_form" value="<?php echo $form; ?>" />
        <input type="hidden" name="job_id" value="<?php echo esc_attr( $job_id ); ?>" />
        <input type="hidden" name="step" value="<?php echo esc_attr( $step ); ?>" />

                    <?php if($job_id){ ?>
                    <img src="https://www.salusa.co.uk/wp-content/uploads/2017/12/ajax-loader.gif" id="img" style="display:none"/ >
                    <input type="submit" id="submitjob" name="submit_job" class="button" value="Update my service profile" />


                    <?php } else { ?>
                     <img src="https://www.salusa.co.uk/wp-content/uploads/2017/12/ajax-loader.gif" id="img" style="display:none"/ >
                    <input type="submit" id="submitjob" name="submit_job" class="button" value="Submit profile for admin approval" />
                    <?php } ?>

    </p>

<script>
                    $('#submitjob').click(function(){
                    $('#img').show();
                    $.ajax({
                    ....
                    success:function(result){
                    $('#img').hide();  //<--- hide again
}
}
</script>

【问题讨论】:

  • 您通过 ajax 发布表单的 php 文件应该有错误。您可以做的是在 ajax 成功功能之后控制台记录结果并检查响应中的任何错误
  • 你阻止表单提交的默认行为吗?

标签: jquery ajax forms


【解决方案1】:

如果您的 Ajax 请求有错误或者您没有为 jquery 包含必要的脚本,则该代码将不起作用。根据您上面所做的,我有一个工作示例,供您参考:

<html>

<head>
  <title>Ajax Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>

<body>
  <input type="hidden" name="job_manager_form" value="<?php echo $form; ?>" />
  <input type="hidden" name="job_id" value="<?php echo esc_attr( $job_id ); ?>" />
  <input type="hidden" name="step" value="<?php echo esc_attr( $step ); ?>" />


  <!-- Here comes your if condition -->
  <img src="https://www.salusa.co.uk/wp-content/uploads/2017/12/ajax-loader.gif" id="img" style="display:none" />
  <input type="submit" id="submitjob" name="submit_job" class="button" value="Update my service profile" />

  <div id="content"></div>
</body>

<script>
  $('#submitjob').click(function() {
    $('#img').show();
    $.ajax({
      url: 'https://jsonplaceholder.typicode.com/posts/1',
      type: 'GET',

      error: function() {
        $('#content').html('<p>An error has occurred</p>');
      },
      dataType: 'json',
      success: function(result) {
        $('#content').html("data Received" + result);
        $('#img').hide(); //<--- hide again
      },
    });
  });
</script>
</body>

</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多