gpt4 book ai didi

javascript - 使用 jquery 文本框值小于两个字母(数字)值在前面加上 0

转载 作者:行者123 更新时间:2023-11-28 15:18:43 25 4
gpt4 key购买 nike

我目前正在使用 jQuery 进行前置。在文本字段中,如果用户在文本字段中输入 2 位数字,则当他在文本字段外部单击时,应自动在文本前添加 0 — 例如,如果用户输入 12 code> 他指出该值应该是 012

使用当前代码,我正在检查他输入了多少个字母,但我很困惑如何进行此附加或串联。

通过此代码,我使用 jquery 验证器验证文本字段

 txt_CC:{
required: true,
number:true,
maxlength:3
},

这是我的 jquery 代码

$('#txt_CC').on('change',function(e){
if($('#txt_CC').val().length > 2){
$('.cc_field').val() + '0';
}else{
alert("Sorry not eligble");
}
});

这是我的输入字段。

<input type="text" class="cc_field" placeholder="Country Code"
id="txt_CC" maxlength="3" name="txt_CC" />

最佳答案

你可以试试这个:

$('#txt_CC').on('change',function(e){
alert($('#txt_CC').val().length);
if($('#txt_CC').val().length <= 2){
$('.cc_field').val('0' + $('.cc_field').val());
}
else{
alert("Sorry not eligble");
}
});

关于javascript - 使用 jquery 文本框值小于两个字母(数字)值在前面加上 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32465608/

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