所以基本上我有一个按钮 .button
,每次按下它时都会在我的输入 #number
中添加一个数字。
现在,当我的输入 #number
中已经有 7 位数字时,我希望函数像“停止工作”一样。
这是我的代码(工作正常):
function nrtwo(hello){
var das = $(hello).html();
var tempNum = $("#number").val();
$("#number").val(tempNum + '' + das);
tempNum = null;
};
$(".button").click(function(){
nrtwo(this);
});
我在想这样的事情?
if ($("#number").attr('maxlength') == '7') {
return false;
}
感谢您的帮助。
最佳答案
试试这个 .length
它是一个 Number
并且当你达到 7 位数时取消盲目 click
事件:
工作 jsFiddle
$(".button").click(function(){
if ($("#number").val().length == 7) {
$(this).unbind('click');
return false;
}else{
nrtwo(this);
}
});
关于javascript - 如果输入已达到 7 位数字,则停止该功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41459625/