使用表单中的 Ajax 脚本指示成功
我有一个表单可以操纵通过此 ajax 脚本发布的图像的大小。该脚本和我的控制器工作正常。我的问题是,如何向此 jquery 脚本附加成功消息?如果我使用常规的 $.ajax() 脚本,那就很容易了。
我通过使用 codeigniter 的 form torch 获得了这个脚本,并且想了解如何用它来指示成功。在我看来,“函数(数据)”似乎表明失败,尽管我不明白它是如何触发的。如果表单提交为空,无论如何它都不起作用。
$(document).ready(function() {
$("#form").submit(function() {
var image = $("#image").val();
$.post("/post/process", { image:image },
function(data){
$("#image_error").html(data.image);
},'json');
});
});//this comes from form torch
控制器是图像处理的标准东西 表格
<?=form_open("/post/process", 'onsubmit="return false;" id="form"')?>
<p>
<label for="image">Image</label>
<span id="image_error" class="error"></span>
<input type="text" name="image" id="image" />
我想在表格底部添加类似的内容
<div id="success"></div>
感谢您的阅读
I have a form that manipulates the size of an image that is posted via this ajax script. The script and my controller work fine. My question is, how do I append a success message to this jquery script? If I used the regular $.ajax() script it would be easy.
I obtained this script through using form torch for codeigniter and would like to learn how to indicate success with it. The "function(data)" appears to me to be there to indicate failure although I dont understand how that is triggered. It doesnt work anyways if the form is submitted empty.
$(document).ready(function() {
$("#form").submit(function() {
var image = $("#image").val();
$.post("/post/process", { image:image },
function(data){
$("#image_error").html(data.image);
},'json');
});
});//this comes from form torch
The controller is the standard stuff for image manipulation
The form
<?=form_open("/post/process", 'onsubmit="return false;" id="form"')?>
<p>
<label for="image">Image</label>
<span id="image_error" class="error"></span>
<input type="text" name="image" id="image" />
I would like to add something like this at the bottom of the form
<div id="success"></div>
Thanks for reading
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就我个人而言,我更喜欢
$.ajax
函数,因为它只为您提供一个可用于所有 AJAX 调用的函数。此外,它还允许您提供两个函数:一个在请求成功时执行,另一个在请求失败时执行。根据文档(http://api.jquery.com/jQuery.post/),
$.post
方法仅在操作成功时调用您指定的函数。另外,如果您需要让脚本在用户执行其他操作之前等待成功或失败的指示,请考虑将
$.ajax
方法与async:false
结合使用范围。有多种方法可以实现您所描述的内容:一种方法是为传递给
$.post
的函数编写类似的内容:Personally, I prefer to you the
$.ajax
function because it gives you just one function you can use for all AJAX calls. In addition, it allows you to provide two functions: one to be executed if the request succeeds and the other to be executed if the request fails.According to the documentation (http://api.jquery.com/jQuery.post/), the
$.post
method only calls the function you specify if the operation succeeds.Also, if you need to make the script wait for an indication of success or failure before the user does anything else, consider using the
$.ajax
method with theasync:false
parameter.There are several ways to accomplish what you described: one way would be to write something like this for the function you pass to
$.post
:如果我没记错的话,你可以做类似的事情:
响应可以保存成功和错误消息。
然后,您可以使用 if 条件检查它并写入您的 div:
If i'm not mistaken, you can do something similar to this:
And response can hold success and error messages.
Than, u can check it using if-condition and write to your div: