命令“show()”在jquery提交表单中不起作用?
最简单的例子是当用户单击“提交”时,我想显示一个 div,其中有一个 gif 表明消息正在发送。
Javascript:
//Send mail
$("div.contato-pedidooracao form").submit(function () {
$(".sending").show(); //HERE.. DONT WORK!! why??
$(".contato-pedidooracao form").hide(); //THIS WORKS!!
var dataString = $(this).serialize();
$.ajax({
type: "POST",
url: "Contato/SendMail",
data: dataString,
dataType: "json",
success: function (data) {
$(".sendMessage").empty();
$(".sendMessage").append("<p>Mensagem enviada com sucesso</p>")
$(".sendMessage").append("<p>Rezaremos por você!</p>")
$(".sendMessage").show();
},
error: function (xhr, ajaxOptions, thrownError) {
$(".sendMessage").empty();
$(".sendMessage").append("<p>Ocorreu um erro ao tentar enviar a mensagem</p>")
$(".sendMessage").append("<p>Por favor, tente novamente mais tarde.</p>")
$(".sendMessage").show();
$(".contato-pedidooracao form").show();
}
});
$(".sending").hide();
event.preventDefault();
});
HTML
<div class="content contato-pedidooracao">
@using (Html.BeginForm())
{
<p>Conte-nos seu pedido</p>
<div class="inline">
@Html.Label("name", "Nome:")
@Html.TextBox("name", "")
</div>
<div class="inline">
@Html.Label("phone", "Telefone:")
@Html.TextBox("phone", "", new { @class = "phone" })
@Html.TextBox("cel", "", new { @class = "phone" })
</div>
<div class="inline">
@Html.Label("email", "e-mail:")
@Html.TextBox("email", "", new { @class = "email" })
</div>
<div class="inline">
@Html.Label("message", "Mensagem:")
@Html.TextArea("message", "")
</div>
<div class="submit">
<input type="submit" value="Enviar" title="Enviar" />
</div>
}
<div class="message sending">
<p>Enviando mensagem</p>
<img src="@Href("~/Images/ajax-loader.gif")" alt="Enviando..." />
</div>
<div class="message sendMessage"></div>
</div>
我不知道它是否相关..但是CSS的一部分:
div.column > div.contato-pedidooracao > div.message
{
display:none;
}
.sending, .sendMessage
{
text-align:center;
}
处理平均需要5秒,但是< code>div.sending 没有出现在这个时间间隔内。
The simplest example is when the user clicks submit, I want to show a div where it has a gif indicating that the message is being sent.
Javascript:
//Send mail
$("div.contato-pedidooracao form").submit(function () {
$(".sending").show(); //HERE.. DONT WORK!! why??
$(".contato-pedidooracao form").hide(); //THIS WORKS!!
var dataString = $(this).serialize();
$.ajax({
type: "POST",
url: "Contato/SendMail",
data: dataString,
dataType: "json",
success: function (data) {
$(".sendMessage").empty();
$(".sendMessage").append("<p>Mensagem enviada com sucesso</p>")
$(".sendMessage").append("<p>Rezaremos por você!</p>")
$(".sendMessage").show();
},
error: function (xhr, ajaxOptions, thrownError) {
$(".sendMessage").empty();
$(".sendMessage").append("<p>Ocorreu um erro ao tentar enviar a mensagem</p>")
$(".sendMessage").append("<p>Por favor, tente novamente mais tarde.</p>")
$(".sendMessage").show();
$(".contato-pedidooracao form").show();
}
});
$(".sending").hide();
event.preventDefault();
});
HTML
<div class="content contato-pedidooracao">
@using (Html.BeginForm())
{
<p>Conte-nos seu pedido</p>
<div class="inline">
@Html.Label("name", "Nome:")
@Html.TextBox("name", "")
</div>
<div class="inline">
@Html.Label("phone", "Telefone:")
@Html.TextBox("phone", "", new { @class = "phone" })
@Html.TextBox("cel", "", new { @class = "phone" })
</div>
<div class="inline">
@Html.Label("email", "e-mail:")
@Html.TextBox("email", "", new { @class = "email" })
</div>
<div class="inline">
@Html.Label("message", "Mensagem:")
@Html.TextArea("message", "")
</div>
<div class="submit">
<input type="submit" value="Enviar" title="Enviar" />
</div>
}
<div class="message sending">
<p>Enviando mensagem</p>
<img src="@Href("~/Images/ajax-loader.gif")" alt="Enviando..." />
</div>
<div class="message sendMessage"></div>
</div>
I do not know if it's relevant .. but part of the CSS:
div.column > div.contato-pedidooracao > div.message
{
display:none;
}
.sending, .sendMessage
{
text-align:center;
}
Processing takes an average of 5 seconds, but the div.sending
does not appear in this time interval..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为AJAX是异步的,JS不会等待。因此,放置在 AJAX 之后的
$(".sending").hide();
实际上是在$(".sending").show();
之后执行的>。尝试将
$(".sending").hide();
放在 AJAX 中成功和错误函数的末尾。It's because AJAX is asynchronous, JS will not wait for it. So the
$(".sending").hide();
placed after the AJAX, is actually executed right after the$(".sending").show();
.Try putting the
$(".sending").hide();
at the end of your success and error functions in the AJAX.