简体   繁体   English

Javascript脚本第一次不起作用,但此后起作用

[英]Javascript script doesn't work the first time but works after that

I am trying to create a commenting system for my site. 我正在尝试为我的网站创建一个评论系统。 A script is used to show massages (or alert) when the comment is submitted. 提交评论时,脚本用于显示消息(或警报)。 I don't have enough knowledge in js to debug and fix this problem myself so I though I would ask here till I learn more about js. 我没有足够的JS知识来自己调试和解决此问题,因此我会在这里询问,直到我进一步了解JS。

My script imports which are in the header: 我的脚本导入位于标头中:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js" async></script>
<script type="text/javascript" src="/js/main.js" async></script>

And my main.js script: 还有我的main.js脚本:

// Static comments
jQuery(document).ready(function ($) {
  var $comments = $('.js-comments');

  $('#comment-form').submit(function () {
    var form = this;

    $(form).addClass('submitting');
    $('#comment-form-submit').html('Loading ...');

    $.ajax({
      type: $(this).attr('method'),
      url: $(this).attr('action'),
      data: $(this).serialize(),
      contentType: 'application/x-www-form-urlencoded',
      success: function (data) {
        $('#comment-form-submit').html('Submitted');
        $('.new-comment-form .js-notice').removeClass('notice--danger').addClass('notice--success');
        showAlert('<p style="color:#47bd40">Thanks for your comment! It will show on the site once it has been approved.</p>');
      },
      error: function (err) {
        console.log(err);
        $('#comment-form-submit').html('Submit Comment');
        $('.new-comment-form .js-notice').removeClass('notice--success').addClass('notice--danger');
        showAlert('<p style="color:#e64848">Submission failed, please fill in all the required inputs.</p>');
        $(form).removeClass('submitting');
      }
    });

    return false;

  });

  function showAlert(message) {
    $('.new-comment-form .js-notice').removeClass('hidden');
    $('.new-comment-form .js-notice-text').html(message);
  }
});

My problem is, if I submit the comment, the script doesn't work, but if I waited for sometime and submitted again, it works just fine. 我的问题是,如果我提交评论,脚本将无法正常工作,但是如果我等待了一段时间然后再次提交,脚本就可以正常工作。

I am guessing it has something to do with loading the scripts and running them, I saw some similar questions here but non of the answers fixed my problem. 我猜它与加载脚本和运行它们有关,我在这里看到了一些类似的问题,但没有答案解决了我的问题。

Async on your script tags means 'Asynchronous'. 脚本标签上的异步表示“异步”。

Usually, when you load a webpage, the browser will request (if not in its updated cache already) all the files and imports that are specified in your main html. 通常,当您加载网页时,浏览器会请求(如果尚未在其更新的缓存中)所有在主html中指定的文件和导入。 One of these things that are loaded at start up is all your script tags. 所有脚本标记都是在启动时加载的东西之一。

But you have async in there. 但是您那里有异步。

So your code is not running because it doesn't exist yet! 因此您的代码未在运行,因为它尚不存在!

Remove async and it should run fine. 删除异步,它应该可以正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM