我遇到了ajax提交代码的问题。
<script type="text/javascript" >
$(function() {
$(".submit").click(function() {
var id = $("#id").val();
var name = $("#name").val();
var description = $("#description").val();
var dataString = 'id='+ id + '&name='+ name + '&description=' + description;
if(id=='' || name=='' || description==''){
$('.alert-success').fadeIn(200).hide();
$('.alert-danger').fadeOut(200).show();
}
else {
$.ajax({
type: "POST",
url: "?act=process",
data: dataString,
success: function() {
$('.alert-success').show();
setTimeout(function () { window.location.href = "?"; }, 3000);
},
error: function() {
$('.alert-danger').show();
setTimeout(function () { window.location.href = "?"; }, 3000);
}
});
}
return false;
});
});
</script>
我需要显示两个警报,并在3秒后隐藏和重定向。 成功警报正常,但错误仅出现,不会隐藏和重定向。
此警报显示的HTML标记如下所示:
<div class="alert alert-success alert-dismissable" style="display:none">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> Success!</div>
<div class="alert alert-danger alert-dismissable" style="display:none">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> Error</div>
问题出在哪里?
答案 0 :(得分:0)
如果以下操作不起作用,问题不在于您的脚本。
jQuery(document).ready(function($){
$.ajax({
url: '?act=process',
type: 'post',
data: {
id: $id,
name:$name,
description:$description
},
success: function (result) {
$('.alert-success').show();
setTimeout(function () { window.location.href = "http://youtube.com/"; }, 3000);
console.log('Yay it worked');
},
error: function (xhr, ajaxOptions, thrownError) {
$('.alert-danger').show();
setTimeout(function () { window.location.href = "http://google.com/"; }, 3000);
console.log('Something went wrong');
}
});
});