Jquery模态窗口只在输入值长度大于2时显示

Jquery modal window show only if input value lenght is more than 2

本文关键字:大于 2时 显示 输入 模态 窗口 Jquery      更新时间:2023-09-26

我有这样的代码显示模态窗口,当我点击进入我的键盘:

  $(".search-input").keypress(function(e) {
    if(e.which == 13) {
      $('#screen').css({ opacity: 0.5, 'width':$(document).width(),'height':$(document).height()});
      $('#screen').show();
      $('#loading_modal').show();    
    }
  });

但我需要自定义它,如果输入类。search-input值小于3,我没有显示任何模态窗口的。

I try so:

  $(".search-input").keypress(function(e) {
    if(e.which == 13) {
      if($(".search-input").value.length > 2) {
        $('#screen').css({ opacity: 0.5, 'width':$(document).width(),'height':$(document).height()});
        $('#screen').show();
        $('#loading_modal').show();    
      }
    }
  });

但由于某些原因它不起作用(如何解决我的问题?

使用trim()删除空白和val()

试试这个

  if($.trim($(this).val()).length > 2) {
      ......

这基本上将替换任何文本:

$(".search-input").value.replace(/{.*?}/g, '').length > 2

试试这个:

$.trim($(this).val().replace(/[^'w's]/g, '')).length > 2

如果你真的只希望在搜索字符串中包含字母,这样做:

$(this).val().replace(/[^a-z]/gi, '').length > 2