gpt4 book ai didi

javascript - 如何将 .val()[0] 用于 2 位数字?

转载 作者:行者123 更新时间:2023-11-30 14:41:36 25 4
gpt4 key购买 nike

我正在处理简单的信用卡脚本,当在输入字段中输入指定的 1 位数字时,它使用 .change() 通过稍微改变 CSS 以及 .val 从 png 图片中显示正确的信用卡公司(Visa、Mastercard、Amex) ()[0] 它只从第一个值或数字读取。

Js :

$( "#ccnr" ).change(function() {
var firstNumber = $('#ccnr').val()[0]; //Problem here
if (firstNumber == 3) {
$("#ccnr").css("background-position", "225px 12%");
} else if (firstNumber == 4) {
$("#ccnr").css("background-position", "225px -0.5%");
} else if (firstNumber == 6) {
$("#ccnr").css("background-position", "225px 18%");
} else if (firstNumber == 5) {
$("#ccnr").css("background-position", "225px 5.7%");
} else {
$("#ccnr").css("background-position", "225px 87%");
}
});

html

<tr>
<td style="padding-bottom:7px"><b><label style="font-size:13px"> Number:</label></b></td>
<td style="padding-bottom:7px">

<input name="ccnr" autocomplete="off" id="ccnr" style="background-image: url(https://image.prntscr.com/image/FBB6i2bDQMmUAKYKY2TFrg.png);background-position:225px 87%;background-repeat: no-repeat;height:22px;width:250px;margin-top:-5px;" type="text">
</td>
</tr>

我怎样才能像这样使用两位数?

JS:

$( "#ccnr" ).change(function() {
var firstNumber = $('#ccnr').val()[0];
if (firstNumber == 37) { //Set to two digit
$("#ccnr").css("background-position", "225px 12%");
} else if (firstNumber == 45) {
$("#ccnr").css("background-position", "225px -0.5%");
} else if (firstNumber == 60) {
$("#ccnr").css("background-position", "225px 18%");
} else if (firstNumber == 55) {
$("#ccnr").css("background-position", "225px 5.7%");
} else {
$("#ccnr").css("background-position", "225px 87%");
}
});

它不工作。

最佳答案

如果你只想提取前 2 个字符:

var firstNumber = $('#ccnr').val().substr(0,2);

使用 .substr(0,2) 可以正常工作。

关于javascript - 如何将 .val()[0] 用于 2 位数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49568815/

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