在尝试了很多之后,我自己修复了它以实现目标。
目标是在输入不完整的情况下保持文本不消失,如果没有给出扩展名,则摆脱 x。
我在代码末尾添加了这个测试:
var splitString = $("[name=myinput]").val().split("x");
//if splitString array length is more than 1 means splitString[1] is set
//if splitString[1].length is 0 means its only x by itself so get rid of it
if (splitString.length > 1 && splitString[1].length == 0) {
$("[name=myinput]").val($("[name=myinput]").val().replace("x", ""));
}
例子:
entered text: 718-111-123 x -> turns into -> 718-111-123
entered text: 718-111 x -> turns into -> 718-111
entered text: 718-111-123 x555 -> turns into -> 718-111-123 x555