gpt4 book ai didi

javascript - 我们如何删除 keyup 上的自定义有效性消息?

转载 作者:行者123 更新时间:2023-12-03 00:06:18 25 4
gpt4 key购买 nike

我的问题是,当我将 customValidity 设置为输入(#zip_code)时,每次更新此输入时它都不会停止显示(我在 keyup 事件中使用它并且使用 setCustomValidity('') 无法解决问题)

我有这个html:

<input type="text"  required placeholder="Nom"         name="lastname"     class="col-md-6 form-control"                                                 />
<input type="text" required placeholder="Prénom" name="firstname" class="col-md-6 form-control" />
<input type="text" required placeholder="Adresse" name="address" class="col-md-12 form-control" />
<input type="text" required placeholder="Code Postal" name="zip_code" class="col-md-6 form-control" id="zip_code" data-inputmask="'mask': '99999'" />
<input type="text" required placeholder="Ville" name="city" class="col-md-6 form-control disabled" id="city" />
<input type="text" required placeholder="Téléphone" name="phone_number" class="col-md-12 form-control" data-inputmask="'mask': '99 99 99 99 99'" />
<input type="email" required placeholder="Email" name="email" class="col-md-12 form-control" />

还有这个 JavaScript:

var zipCode = "";
$('#zip_code').keyup(function() {
zipLength = zipCode.split('_')[0].length
if (zipLength < 5 || zipLength == 5 && $( this ).val().split('_')[0].length != 5) {
zipCode = $( this ).val();
$('#city').replaceTag('<input>', true);
$('#city').addClass('disabled');
$('#city').focus(function(){$("#zip_code").focus();});
$('#zip_code')[0].setCustomValidity("Veuillez renseigner un code postal valide.");
$('#city').attr('tabindex', -1);
$('#city').attr("placeholder", zipCode.length == 0 ? "Ville" : "Aucune ville pour ce code postal");
$('#city').html('');
if (zipCode.length == 5) {
$.ajax( {
url: "an url to a json representating every city having the zipCode",
dataType: "json",
method: "POST",
data: {
zipCode: zipCode
},
success: function( data ) {
data.cities.map( function( x, i ) {
let cityName = x.name.charAt(0) + x.name.substr(1).toLowerCase().trim();
if (i == 0) {
if (data.cities.length > 1) {
$('#city').removeClass('disabled');
$('#city').replaceTag( '<select>', true );
$("#city").append( new Option( cityName, cityName, true ) );
} else {
$('#city').addClass('disabled');
$('#zip_code')[0].setCustomValidity("Veuillez renseigner un code postal valide.");
$('#city').focus(function(){$("#zip_code").focus();});
$('#city').val( cityName );
}
} else {
$("#city").append( new Option( cityName, cityName ) );
}
} );
}
} );
}
}
});
$(":input").inputmask();

正如你所看到的,我正在使用 inputmask 模块。还有一些完全禁用 #city 输入的技巧(没有直接焦点,没有制表,也没有关注无效性)我必须自己“禁用”,因为“禁用”属性也会禁用“必需”属性。我想我可以通过使用空的 customValidity 以某种方式修补它,但我不知道如何。

最佳答案

像这样的......我想我忘记了一些关闭的if,但这个想法是将验证与keyup分开,并在表单提交时检查它

var zipCode = "";
$("#your-form-id").submit(function(e){
e.preventDefault();
zipLength = zipCode.split('_')[0].length

if (zipLength < 5 || zipLength == 5 && $( this ).val().split('_')[0].length != 5) {
zipCode = $( this ).val();
$('#city').replaceTag('<input>', true);
$('#city').addClass('disabled');
$('#city').focus(function(){$("#zip_code").focus();});
$('#zip_code')[0].setCustomValidity("Veuillez renseigner un code postal
valide.");
$('#city').attr('tabindex', -1);
$('#city').attr("placeholder", zipCode.length == 0 ? "Ville" : "Aucune
ville pour
ce code postal");
$('#city').html('');
return false; // it make the form no submit, if postal code is wrong then the form dont submit
}
});

$('#zip_code').keyup(function() {



if (zipCode.length == 5) {
$.ajax( {
url: "an url to a json representating every city having the zipCode",
dataType: "json",
method: "POST",
`enter code here` data: {
zipCode: zipCode
},
success: function( data ) {
data.cities.map( function( x, i ) {
let cityName = x.name.charAt(0) + x.name.substr(1).toLowerCase().trim();
if (i == 0) {
if (data.cities.length > 1) {
$('#city').removeClass('disabled');
$('#city').replaceTag( '<select>', true );
$("#city").append( new Option( cityName, cityName, true )
);
} else {
$('#city').addClass('disabled');
$('#zip_code')[0].setCustomValidity("Veuillez renseigner
un code postal valide.");
$('#city').focus(function(){$("#zip_code").focus();});
$('#city').val( cityName );
}
} else {
$("#city").append( new Option( cityName, cityName ) );
}
} );
}
} );
}
}
});
$(":input").inputmask();

关于javascript - 我们如何删除 keyup 上的自定义有效性消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54948101/

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