showAlert = false;
$('input').keyup(function(){
var value = $(this).val();
if (value.indexOf('+') == 0) {
$(this).val(''); // clear input
showAlert=true; // alert user
if (showAlert==true)
{
console.log ("Phone number must start with +");
showAlert = false;
}
}
});
我希望仅使用+
将电话号码输入归档,但仅显示一次警报...我的代码似乎无法正常工作。
答案 0 :(得分:1)
您错过了(this).val('');
之前的$,此处您不需要标记,条件使用!=
而不是==
。如果您想显示提醒,则应使用alert()
代替console.log
$('input').keyup(function(){
var value = $(this).val();
if (value.indexOf('+') != 0) {
$(this).val(''); // clear input
console.log ("Phone number must start with +");
}
});
答案 1 :(得分:1)
这里要测试的条件必须是:
if (value.indexOf('+') !== 0) {
你不需要旗帜:
$('input').keyup(function(){
if ($(this).val().indexOf('+') !== 0) {
$(this).val(''); // clear input
console.log ("Phone number must start with +");
}
});
答案 2 :(得分:1)
请查看此链接
您应该与!=" 0"
匹配这应该是你的Jquery
var show="";
$('input').keyup(function(){
var value = $(this).val();
if (value.indexOf('+') != 0) {
$(this).val('');
if(show=="")
{
alert("Phone number must start with +");
}
show="1";
}
});
因为您只需要提醒一次