我有一个通过Ajax提交的表单。用户发送表单后,文本更改显示表单已成功发送,然后显示填写的表单。我想显示表单,但我不希望他们重新提交表单,所以我想禁用输入和提交按钮。我尝试将$('#submit_btn').className +=" disabled"
添加到ajax脚本中,但它只是刷新页面而不提交任何内容。
ajax脚本如下:
$(function() {
$('.error').hide();
$('input.text-input').css({backgroundColor:"#FFFFFF"});
$('input.text-input').focus(function(){
$(this).css({backgroundColor:"#FFDDAA"});
});
$('input.text-input').blur(function(){
$(this).css({backgroundColor:"#FFFFFF"});
});
$(".button").click(function() {
// validate and process form
// first hide any error messages
$('.error').hide();
var name = $("input#name").val();
var email = $("inputemail").val();
var phone = $("inputphone").val();
var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "http://www.green-panda.com/website/panda/webParts/contact-form.php",
data: dataString,
success: function() {
$('#myModalLabel').html("<h3 class='text-success' id='myModalLabel'>Contact Form Submitted!</h3>")
$('#myModalSmall').html("<p class='muted'>Your submiessions are below. We will be contacting you soon, you may now close this window.</p>")
$('#submit_btn').className +=" disabled"
.hide()
.fadeIn(1500, function() {
$('#message').append("<i class='icon-ok icon-white'></i>");
});
}
});
return false;
});
});
runOnLoad(function(){
$("input#name").select().focus();
});
成功提交表单后,我怎么可能禁用输入和按钮?
答案 0 :(得分:3)
实际上它比你想做的要简单得多,你不需要禁用输入,只需在ajax请求后取消提交:
$('form').submit(function(){
return false;
});
将其放入ajax请求的成功处理程序中。
如果要禁用提交按钮,请替换以下错误:
$('#submit_btn').className +=" disabled"
使用:
$('#submit_btn').prop("disabled", true);
答案 1 :(得分:0)
在我的应用程序中,我只是禁用了提交按钮并显示了一些进度消息
function submitForm(formObj) {
// Validate form
// if (formObj.email.value === '') {
// alert('Please enter a email');
// return false;
// }
formObj.submit.disabled = true;
formObj.submit.value = 'Log In...';
return true;
}
<form class="form-login" accept-charset="UTF-8"
method="POST" action="/login" onsubmit="return submitForm(this);">
......
<input type="submit" id="login-submit" name="submit" value="Log In">
</form>