Javascript jQuery添加到输入,但只添加两次
我这里有一个小脚本。。。它在每4个字符后添加一个“-”,但我需要它在12个字符后不添加-Javascript jQuery添加到输入,但只添加两次,javascript,jquery,Javascript,Jquery,我这里有一个小脚本。。。它在每4个字符后添加一个“-”,但我需要它在12个字符后不添加- $(function(){ $("#promo").keyup(function(){ var $this = $(this); if ((($this.val().length+1) % 5)==0){ $this.val($this.val() + "-"); } }); }); 但是它在12个字符的末尾加了一个“-”,尽管
$(function(){
$("#promo").keyup(function(){
var $this = $(this);
if ((($this.val().length+1) % 5)==0){
$this.val($this.val() + "-");
}
});
});
但是它在12个字符的末尾加了一个“-”,尽管我的字符限制是14个
我有一个
如何防止这种情况发生?如果:
if ($this.val().length > 12) {
return false;
}
代码建议(使用较少的jQuery):
演示如果:
if ($this.val().length > 12) {
return false;
}
代码建议(使用较少的jQuery):
演示
maxlength
属性不会阻止您使用Javascript修改
值
您仍然可以添加支票:
$(function(){
$("#promo").keyup(function(){
var $this = $(this);
if ((($this.val().length+1) % 5)==0 && $this.val().length() < $this.attr("maxlength")){
$this.val($this.val() + "-");
}
});
});
$(函数(){
$(“#promo”).keyup(函数(){
var$this=$(this);
如果((($this.val().length+1)%5)==0&&$this.val().length()<$this.attr(“maxlength”)){
$this.val($this.val()+“-”);
}
});
});
maxlength
属性不阻止您使用Javascript修改
值
您仍然可以添加支票:
$(function(){
$("#promo").keyup(function(){
var $this = $(this);
if ((($this.val().length+1) % 5)==0 && $this.val().length() < $this.attr("maxlength")){
$this.val($this.val() + "-");
}
});
});
$(函数(){
$(“#promo”).keyup(函数(){
var$this=$(this);
如果((($this.val().length+1)%5)==0&&$this.val().length()<$this.attr(“maxlength”)){
$this.val($this.val()+“-”);
}
});
});
您只需使用&&AND运算符,并将其设为一个条件,即如果val.length大于11,则不会发生任何事情
$(function(){
$("#promo").keyup(function(){
var $this = $(this);
if ((($this.val().length+1) % 5)==0 && $this.val().length <=11){
$this.val($this.val() + "-");
}
});
});
$(函数(){
$(“#promo”).keyup(函数(){
var$this=$(this);
如果(($this.val()
$(function(){
$("#promo").keyup(function(){
var $this = $(this);
if ((($this.val().length+1) % 5)==0 && $this.val().length <=11){
$this.val($this.val() + "-");
}
});
});
$(函数(){
$(“#promo”).keyup(函数(){
var$this=$(this);
如果(($this.val().length+1)%5)=0&&$this.val().length
$(function(){
$("#promo").keyup(function(){
var $this = $(this);
if ((($this.val().length+1) % 5)==0 && $this.val().length <= 12){
$this.val($this.val() + "-");
}
});
});
$(函数(){
$(“#promo”).keyup(函数(){
var$this=$(this);
如果(($this.val().length+1)%5)=0&&$this.val().length
$(function(){
$("#promo").keyup(function(){
var $this = $(this);
if ((($this.val().length+1) % 5)==0 && $this.val().length <= 12){
$this.val($this.val() + "-");
}
});
});
$(函数(){
$(“#promo”).keyup(函数(){
var$this=$(this);
如果(($this.val().length+1)%5)==0&&$this.val().length$(函数(){
$(“#promo”).keyup(函数(){
var$this=$(this);
if($this.val().length$(函数(){
$(“#promo”).keyup(函数(){
var$this=$(this);
如果($this.val()LimthWoW,在Fiddle也做了这个。伙计,这个网站上的帮助人太多了!哈!谢谢@ GuiuuMePousEL!我的JQuery和JS仍然很基本!如果你的问题解决了,请考虑一下。对不起,他们花了我一分钟时间来做这件事……GuillaumePousselWow,也只是在Fiddle做的。伙计,太多了。这个网站上有帮助的人!哈!谢谢@ GuiuouMePousEL!我的JQuery和JS仍然很基本!如果你的问题解决了,请考虑一下。对不起,他们花了我一分钟时间来做这件事……