gpt4 book ai didi

javascript - 在 Bootstrap Validator Form 中停止默认表单提交

转载 作者:数据小太阳 更新时间:2023-10-29 04:58:08 24 4
gpt4 key购买 nike

我已经看到这个问题问过几次了,但它们似乎并不适用于我的情况。

在验证测试之前提交我的表单。

形式:

<form accept-charset="UTF-8" action="/user/fact" data-bv-submitbuttons="button[type='submit']" data-remote="true" data-toggle="validator" id="user_fact_form" method="post" role="form"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div>
<div class="form-group">
<label for="key">Title</label>
<input class="form-control" id="key" name="key" type="text" value="" />
</div>
<div class="form-group">
<label for="value">Value</label>
<input class="form-control" id="value" name="value" type="text" value="" />
</div>
<div class="form-group">
<input class="btn btn-primary" disable="true" name="commit" type="submit" value="Add" />
</div>
</form>

JavaScript:

$('#user_fact_form').bootstrapValidator({
live: 'enabled',
message: 'This value is not valid',
submitButton: '$user_fact_form button[type="submit"]',
submitHandler: function(validator, form, submitButton) {
$.post(form.attr('action'), form.serialize(), function (result) {
$("#facts_tbody").append(result.data);
});
return false;
},
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
key: {
selector: '#key',
validators: {
notEmpty: {
message: 'The title is required and cannot be empty'
}
}
},
value: {
selector: '#value',
validators: {
notEmpty: {
message: 'The value is required and cannot be empty'
}
}
}
}
});

有什么想法可以在验证表单之前禁用提交按钮吗?

最佳答案

最后,我需要做的是使用 action="javascript:return;" 禁用表单操作并使用 Jquery AJAX 方法发布数据:

$('#user_fact_form').bootstrapValidator({
live: 'enabled',
message: 'This value is not valid',
submitButton: '$user_fact_form button[type="submit"]',
submitHandler: function(validator, form, submitButton) {
$.ajax({
type: 'POST',
url: 'my_form.url',
data: $(form).serialize(),
success: function(result) {
$("#facts_tbody").append(result);
$("#key").val('');
$("#value").val('');
$("#user_fact_form").data('bootstrapValidator').resetForm();
}
});
return false;
},
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
key: {
selector: '#key',
validators: {
notEmpty: {
message: 'The title is required and cannot be empty'
}
}
},
value: {
selector: '#value',
validators: {
notEmpty: {
message: 'The value is required and cannot be empty'
}
}
}
}
});

关于javascript - 在 Bootstrap Validator Form 中停止默认表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24376727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com