jQuery $.when() .then() 不适合我

发布于 2025-01-02 00:21:09 字数 1785 浏览 0 评论 0原文

$.when() .then() 对我不起作用,我做错了什么?预先感谢您的帮助。我想确保首先加载表单,然后在表单内填充

以显示 Google 的验证码。这是测试:它实际上加载了表单,但警报根本没有弹出。如果测试成功,我将更改“警报”以调用 showRecaptcha 函数。

<script type="text/javascript">
$("document").ready(function () {
    function showRecaptcha(element) {
           Recaptcha.create("publick_key_here", element, {
             theme: "red",
             callback: Recaptcha.focus_response_field});
   };

  // load the form
  $.get('../php/formularioDeReserva.html', function (data) {
    $.when($('.subscriptionForm').html(data))
       .then(function(){alert('Then')})
       .fail(function(){alert( 'failed.' )});
    alert('I reached this point.');
  });

})
</script>

Felix 注意:我的测试表明 .html(data) 不是立即完成的。在下面的代码中,如果我保留 alert('Load was returned.') ,则警报暂停一段时间,以便 showRecaptcha 成功。如果我删除警报,则没有任何作用。

$.get('../php/formularioDeReserva.html', function (data) {
        $('.subscriptionForm').html(data);
         alert('Load was performed.');
         showRecaptcha('recaptcha_div');
        });

Guiherme的注意事项:尝试你的建议,在我看来这是一个很好的逻辑想法,但仍然不起作用,我不明白为什么不行。在以下代码中,显示了 subcritionForm,但根本不弹出 successerrorcomplete 警报。

$.get('../php/formularioDeReserva.html', function (data) {
        $('.subscriptionForm').html(data);
         // alert('Load was performed.');
         // showRecaptcha('recaptcha_div');
    })
    .success(function() { alert("second success"); })
    .error(function() { alert("error"); })
    .complete(function() { alert("complete"); });

注意:什么时候“查看源代码”我意识到 Java 脚本被包含了两次,因为其中一个文件没有正确保存。这就解释了为什么警报会弹出两次。

$.when() .then() is not working for me, what am I doing wrong? Thanks in advance for your help. I want to make sure the form is loaded first then I want to populate a <div> inside the form to show Google's recaptcha. This is the test: It actually loads the form but the alerts are not poping up at all. I the test works I'll change the "alerts" to call the showRecaptcha function.

<script type="text/javascript">
$("document").ready(function () {
    function showRecaptcha(element) {
           Recaptcha.create("publick_key_here", element, {
             theme: "red",
             callback: Recaptcha.focus_response_field});
   };

  // load the form
  $.get('../php/formularioDeReserva.html', function (data) {
    $.when($('.subscriptionForm').html(data))
       .then(function(){alert('Then')})
       .fail(function(){alert( 'failed.' )});
    alert('I reached this point.');
  });

})
</script>

Note for Felix: My tests show that .html(data) is not done instantaneously. In the following code if I leave the alert('Load was performed.') the pause of the alert give some time so the showRecaptcha succeds. If I remove the alert nothing works.

$.get('../php/formularioDeReserva.html', function (data) {
        $('.subscriptionForm').html(data);
         alert('Load was performed.');
         showRecaptcha('recaptcha_div');
        });

Note for Guiherme: Trying what you suggested, which seems to me a good logical idea, still does not work, and I don't understand why not. In the following code the subcritionForm shows but the success, error and complete alerts do not pop up at all.

$.get('../php/formularioDeReserva.html', function (data) {
        $('.subscriptionForm').html(data);
         // alert('Load was performed.');
         // showRecaptcha('recaptcha_div');
    })
    .success(function() { alert("second success"); })
    .error(function() { alert("error"); })
    .complete(function() { alert("complete"); });

Note: When did a "view source" I realized that the Java script was included twice because one of the file was not saving properly. That explained why the alerts were popping twice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃梦幻 2025-01-09 00:21:09

您需要在 the

$.when()

之前

$.get()

调用 the ,但是,如果它只是一个 get 请求,为什么不使用

$.ajax({...}).success(function(data){..}).error(function(){...})...;

甚至

   $.get({...})
    .success(function() { alert("second success"); })
    .error(function() { alert("error"); })
    .complete(function() { alert("complete"); });

那样,我认为在这种情况下永远不会调用 failed() 。

You need to call the

$.when()

before the

$.get()

but, if it just one get request, why don't use

$.ajax({...}).success(function(data){..}).error(function(){...})...;

or

   $.get({...})
    .success(function() { alert("second success"); })
    .error(function() { alert("error"); })
    .complete(function() { alert("complete"); });

even that, I think the fail() will never be call in that case.

溇涏 2025-01-09 00:21:09

我放弃了!!!

我决定将主 .html 文件重命名为 .php 并使用 php include 语句而不是 >.get

<div class="subscriptionForm"> 
     <?php include("../php/formularioDeReserva.html"); ?>
</div> 

并将 jQuery 更改为简单的调用以显示 recaptcha:

$("document").ready(function () {
  function showRecaptcha(element) {
       Recaptcha.create("public key here", element, {
         theme: "red",
         callback: Recaptcha.focus_response_field});
      };

 showRecaptcha('recaptcha_div');

 [..more here..]

});

感谢大家的帮助。

更新:我刚刚意识到,也许我需要创建一个延期并创建一个承诺,但我并不真正理解这个概念,并且最后使用 php include 就很容易了。

I gave up !!!

I decided to rename the main .html file to .php and use an php include statement instead of the .get:

<div class="subscriptionForm"> 
     <?php include("../php/formularioDeReserva.html"); ?>
</div> 

and changed the jQuery to a simple calling to show recaptcha:

$("document").ready(function () {
  function showRecaptcha(element) {
       Recaptcha.create("public key here", element, {
         theme: "red",
         callback: Recaptcha.focus_response_field});
      };

 showRecaptcha('recaptcha_div');

 [..more here..]

});

Thanks all for your help.

Update: I have just realized that perhaps I needed to create a deferral and create a promise, but I don't really understand the concept and at the end it was easy just to use an php include.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文