jQuery: 如果在一定时间内未填写,则清除输入字段?
jQuery: Clear input field if it is not filled in certain time?
使用jquery或js,如果在特定时间内没有达到x个字符数,如何清除输入字段?
基本上,如果输入字段在 1 秒内未达到 10 个字符,我想清除输入的所有内容。
如何计算从开始输入(输入第一个字符)到输入第 10 个字符的时间?
您可以利用 onfocus
事件和 setTimeout
。
document.getElementById("test").onfocus = function(){
setTimeout(function(th){
if(th.value.length <10){//If the no. of characters is less than 10 then clear the input field
th.value = '';
}
}, 1000, this)//Time set is 1 second
}
<input id="test"/>
也许你应该 link 像这里这样的 keyup
活动:
// ... somewhere in the ON DOM READY section, input has ID "a"
$('#a').keyup(function(){var o=$(this);// o: jQuery object referring to the current input
setTimeout(function(){ if (o.val().length<5) o.val('');},1000);
})
请参阅此处:http://jsfiddle.net/1fjyezf7/。我在示例中将最小长度减少到 5 个字符。不然我打字太慢了;-).
使用jquery或js,如果在特定时间内没有达到x个字符数,如何清除输入字段?
基本上,如果输入字段在 1 秒内未达到 10 个字符,我想清除输入的所有内容。
如何计算从开始输入(输入第一个字符)到输入第 10 个字符的时间?
您可以利用 onfocus
事件和 setTimeout
。
document.getElementById("test").onfocus = function(){
setTimeout(function(th){
if(th.value.length <10){//If the no. of characters is less than 10 then clear the input field
th.value = '';
}
}, 1000, this)//Time set is 1 second
}
<input id="test"/>
也许你应该 link 像这里这样的 keyup
活动:
// ... somewhere in the ON DOM READY section, input has ID "a"
$('#a').keyup(function(){var o=$(this);// o: jQuery object referring to the current input
setTimeout(function(){ if (o.val().length<5) o.val('');},1000);
})
请参阅此处:http://jsfiddle.net/1fjyezf7/。我在示例中将最小长度减少到 5 个字符。不然我打字太慢了;-).