【问题标题】:wanted to extend jQuery to handle a custom enter key event based on tabindex想扩展 jQuery 来处理基于 tabindex 的自定义输入键事件
【发布时间】:2011-01-08 16:50:30
【问题描述】:

我写了下面的代码来处理当输入键按下一个很少验证的时候,如果它是一个文本区域,只允许四行,如果值为空,关注它本身。

var temp = 1;
 function getLines(id)
    {
      return temp=temp+id;
    }
$("#theform").keypress(function(e){
var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;                                
    if (key == 13) {
        var $targ = $(e.target);
        var tindex = $targ.attr("tabindex");
        var count =1;
        var focusNext = false;
        var allowedNumberOfLines = 4;
       if ($targ.is("textarea") && !$targ.is(":button,:submit")) {
          var lines= getLines(count);
            if (lines > allowedNumberOfLines)
            {
            $("#login_error").css('background', '#F7F77C').fadeIn(1000).html("Only "+allowedNumberOfLines+" Lines Allowed").fadeOut(1000);
            tindex++;
            $("[tabindex=" + tindex + "]").focus();    
            return false;
            }
        }
        else if($targ.val() =='' || $targ.val() == "undefined")
        {
        $("[tabindex=" + tindex + "]").focus();    
         return false;
        }
        else if($targ.val() !='' || $targ.val() != "undefined")
        {
        tindex++;
        $("[tabindex=" + tindex + "]").focus();    
         return false;
        }
    }
});

有没有办法让它成为一个自定义函数,这样我就可以像这样调用函数了

$('theform').returnPress();

【问题讨论】:

  • 你想让 returnPress() 做什么?

标签: jquery key tabindex enter


【解决方案1】:

我今天早些时候为similar question 创建了一个插件。 Check it out!

这不会进行任何验证,但也许您可以自己更改它。

【讨论】:

  • 仅作记录 - 您回答的“类似问题”与此问题来自同一用户。
  • @Matt,哈哈。只是回忆了这个问题,并没有费心检查用户。哦,好吧,吸取教训。感谢您的提醒;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-17
相关资源
最近更新 更多