[英]bootstrap form validation don't work when submit from jquery
I have this api that call some ajax and run asynchronous and i can't submit the form until she gives me an response.我有这个调用一些ajax并异步运行的api,在她给我回复之前我无法提交表单。 So what i did was submit by jquery event just when this api gives me a response.所以我所做的就是在这个 api 给我一个响应时通过 jquery 事件提交。
function getCardToken(card_num, cvv, month_exp, year_exp){
PagSeguroDirectPayment.createCardToken({
cardNumber: card_num, // Número do cartão de crédito
brand: brand, // Bandeira do cartão
cvv: cvv, // CVV do cartão
expirationMonth: month_exp, // Mês da expiração do cartão
expirationYear: year_exp, // Ano da expiração do cartão, é necessário os 4 dígitos.
success: function(response) {
$("#card-token").val(response['card']['token']);
var names_id = new Array("number", "cvv", "expiration");
console.log('si');
$("#pagamento-invalido").hide();
for (var i = 0; i < names_id.length; i++){
$("#cc-"+names_id[i]).addClass("is-valid");
$("#cc-"+names_id[i]).removeClass("is-invalid");
$("#cc-"+names_id[i]).siblings('.invalid-feedback').hide();
}
$("form").trigger("submit");
},
error: function(response) {
console.log('no');
var names_id = new Array("number", "cvv", "expiration");
$("#pagamento-invalido").show();
for (var i = 0; i < names_id.length; i++){
$("#cc-"+names_id[i]).addClass("is-invalid");
$("#cc-"+names_id[i]).removeClass("is-valid");
$("#cc-"+names_id[i]).siblings('.invalid-feedback').show();
}
$("form").trigger("submit");
// $("form").submit();
},
});
}
This is the api code.这是api代码。 When i get a response i submit the form, the thing is that i am using bootstrap validation, and isn't working anymore, it don't call the validation.当我收到回复时,我提交了表单,问题是我正在使用引导程序验证,并且不再工作,它不会调用验证。
(function () {
'use strict'
window.addEventListener('load', function () {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName('needs-validation')
// Loop over them and prevent submission
Array.prototype.filter.call(forms, function (form) {
form.addEventListener('submit', function (event) {
$("#cpf").trigger("blur");
var cpf_invalid = document.getElementById("cpf").classList.contains("is-invalid");
var cep_invalid = document.getElementById("cep").classList.contains("is-invalid")
if (form.checkValidity() === false | cpf_invalid | cep_invalid) {
$("#cep").trigger("blur");
event.preventDefault()
event.stopPropagation()
}
else if($("#card-token").val() === "" | $("#sender-hash") === ""){
event.preventDefault()
event.stopPropagation()
}
console.log($("#card-token").val())
event.preventDefault()
event.stopPropagation()
form.classList.add('was-validated');
}, false)
})
}, false)
}());
So, this is the validation code.所以,这是验证码。 As i said when i submit by jquery it calls this validation, but it simply don't get there.正如我所说,当我通过 jquery 提交时,它会调用此验证,但它根本无法到达那里。
There is a way to fix this?有办法解决这个问题吗? I also tried to use the submit event of the jquery:我也尝试使用jquery的提交事件:
$(document).ready(function() {
$("form").submit(function(e){
$("#cpf").trigger("blur");
var cpf_invalid = document.getElementById("cpf").classList.contains("is-invalid");
var cep_invalid = document.getElementById("cep").classList.contains("is-invalid")
console.log($(this).checkValidity())
if ($(this).checkValidity() === false | cpf_invalid | cep_invalid) {
$("#cep").trigger("blur");
e.preventDefault()
}
else if($("#card-token").val() === "" | $("#sender-hash") === ""){
e.preventDefault()
}
$(this).addClass('was-validated');
e.preventDefault();
});
});
But the $(this).checkValidity()
also don't work and don't stop the submit.但是$(this).checkValidity()
也不起作用并且不会停止提交。
Obs: The submission button it's calling the function that call the api. Obs:提交按钮它正在调用调用api的函数。
I get it, i used the jquery method, but instead of use $(this)
, i used form = $(this)[0]
and then just replaced the values on the code.我明白了,我使用了 jquery 方法,但我没有使用$(this)
,而是使用$(this)
form = $(this)[0]
,然后只是替换了代码中的值。
Link where I found the answer: Bootstrap 4 Form Validation - Custom styling not working after checkValidity() call我找到答案的链接: Bootstrap 4 表单验证 - 在 checkValidity() 调用后自定义样式不起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.