Javascript 如果在输入中插入两个数字字符,则显示/隐藏div

Javascript 如果在输入中插入两个数字字符,则显示/隐藏div,javascript,jquery,html,string,Javascript,Jquery,Html,String,如果插入了两个数字字符,我想显示/隐藏一个div。我从下面开始,但如果插入两个字符,则需要合并 $('input[name=amount]').keyup(function(){ if ($(this).val().length) $('#yeah').show(); else $('#yeah').hide(); }); 所以,;如果至少插入了两个数字字符>.show()else.hide()使用.match()中的正则表达式匹配输入值中的数字并检查匹配的长度 $('

如果插入了两个数字字符,我想显示/隐藏一个div。我从下面开始,但如果插入两个字符,则需要合并

$('input[name=amount]').keyup(function(){
  if ($(this).val().length)
    $('#yeah').show();
  else
    $('#yeah').hide();
});
所以,;如果至少插入了两个数字字符>
.show()
else
.hide()

使用
.match()
中的正则表达式匹配输入值中的数字并检查匹配的长度

$('input[name=amount]').keyup(function(){
  var match = $(this).val().match(/\d/g);
  if (match != null && match.length > 1)
    $('#yeah').show();
  else
    $('#yeah').hide();
}).keyup();
您还可以简化代码并使用
.toggle()

$('input[name=amount]').keyup(function(){
var match=$(this.val().match(/\d/g);
$('#yes').toggle(match!=null&&match.length>1);
}).keyup()


是的
您尝试了什么?您还想要
输入
事件,而不是keyup您的意思是至少插入了两个数字字符吗?你是说这是执行所有代码的条件,还是说这是使“yeah”元素可见的条件?是否允许使用其他字符?如果是,这两个数字应该在开头,还是连续的,或者在任何地方?是的,如果至少插入了两个数字字符>显示div。否则隐藏div。
$('input[name=amount]').keyup(function(){
  var match = $(this).val().match(/\d/g);
  $('#yeah').toggle(match != null && match.length > 1);
}).keyup();