服务器端方法有效,而AJAX函数集data-duplicated="false"
但$this.submit();
不起作用。要实际提交表单,我需要再次按下sumbit按钮。
<form data-url="/.../" data-duplicated="true" onsubmit="return checkForDuplicates(this)">
<!-- Form inputs -->
</form>
function checkForDuplicates(e) {
var $this = $(e);
var url = $this.attr('data-url');
if ($this.attr('data-duplicated') === 'true') {
$.ajax({
url: url,
type: 'POST',
data: data,
success: function (result) {
if (result === true) {
$this.attr('data-duplicated', 'false');
$this.submit();
} else {
// warrning
}
}
});
return false;
}
return true;
}
我该怎么办才能真正提交表格?
答案 0 :(得分:0)
<强> demo link 强>
JavaScript代码
$('input[type="submit"]').click(function(e) {
e.preventDefault();
checkForDuplicates(this);
})
function checkForDuplicates(e) {
var _form = $(e).closest('form');
var url = _form.attr('data-url');
if (_form.attr('data-duplicated') === 'true') {
var data = {};
var url = '/';
$.ajax({
url: url,
type: 'POST',
data: data,
success: function(result) {
if (result === true) {
_form.attr('data-duplicated', 'false');
_form.trigger('submit');
} else {
// warrning
}
}
});
}
}
HTML code:
<form data-url="/.../" data-duplicated="true">
<input type="submit">
</form>
答案 1 :(得分:-1)
$(document).ready(function(){
$('#submit').on('click', function(){
$.ajax({
url: url,
type: 'POST',
data: data,
success: function (result) {
if(result == 'duplicate'){
$('#form').submit();
}else{
//your custom message here
}
}
});
});
});
&#13;
<form id='form' action='' method='post'>
<!--your inputs here-->
<a id='submit'>Submit form</a>
</form>
&#13;
希望它对你有所帮助。