jQuery $.when() .then() 不适合我
$.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,但根本不弹出 success
、error
和 complete
警报。
$.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在 the
之前
调用 the ,但是,如果它只是一个 get 请求,为什么不使用
甚至
那样,我认为在这种情况下永远不会调用 failed() 。
You need to call the
before the
but, if it just one get request, why don't use
or
even that, I think the fail() will never be call in that case.
我放弃了!!!
我决定将主
.html
文件重命名为.php
并使用 php include 语句而不是>.get
:并将 jQuery 更改为简单的调用以显示 recaptcha:
感谢大家的帮助。
更新:我刚刚意识到,也许我需要创建一个
延期
并创建一个承诺
,但我并不真正理解这个概念,并且最后使用 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
:and changed the jQuery to a simple calling to show recaptcha:
Thanks all for your help.
Update: I have just realized that perhaps I needed to create a
deferral
and create apromise
, but I don't really understand the concept and at the end it was easy just to use an php include.