我需要能够在提交时用欧芹验证一个表单,但是延迟实际提交本身,直到其他一些(定时)操作完成。
我试过这个:
$("#myform").on('submit', function(e){
e.preventDefault();
var form = $(this);
form.parsley().validate();
if (form.parsley().isValid()){
// do something here...
setTimeout(function() {
form.submit();
}, 3000);
}
});您可能会猜到,form.submit()只是将我发送到一个无限循环中。我无法确定如何在延迟后触发提交,而不回顾验证。为了澄清,我需要:
有什么想法吗?是否有一种欧芹特定的方法可以提交表单而不进行重新验证?
发布于 2015-06-02 15:48:01
根据this question,一旦一个动作被取消(使用preventDefault()),唯一的选择就是再次触发它。
你已经这么做了。您需要在逻辑中添加的是是否停止事件的条件。你可以用这样的东西:
$(document).ready(function() {
$("form").parsley();
// By default, we won't submit the form.
var submitForm = false;
$("#myform").on('submit', function(e) {
// If our variable is false, stop the default action.
// The first time 'submit' is triggered, we should prevent the default action
if (!submitForm) {
e.preventDefault();
}
var form = $(this);
form.parsley().validate();
// If the form is valid
if (form.parsley().isValid()) {
// Set the variable to true, so that when the 'submit' is triggered again, it doesn't
// prevent the default action
submitForm = true;
// do something here...
setTimeout(function() {
// Trigger form submit
form.submit();
}, 3000);
} else {
// There could be times when the form is valid and then becames invalid. In these cases,
// set the variable to false again.
submitForm = false;
}
});
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/parsley.js/2.0.7/parsley.min.js"></script>
<form id="myform">
<input type="text" name="field" required />
<input type="submit" />
</form>
发布于 2015-06-02 20:27:06
我正在积极地进行一个更新,这将处理承诺,所以您想要实现的将是非常容易做到的。
与此同时,这件事更难做。我认为使用remote版本,您可以按以下方式发送提交事件:
$('.your-form').trigger($.extend($.Event('submit'), { parsley: true }))https://stackoverflow.com/questions/30596315
复制相似问题