【问题标题】:jQuery binding to an element by idjQuery 通过 id 绑定到元素
【发布时间】:2010-11-25 09:54:31
【问题描述】:

我想要做的是当 esc 键按下文本 输入字段(id = txtSearch),一些代码将运行。

这是我的代码:

$(document).ready(function() { 
    $('#txtSearch').bind('keyup', function(e) { 
        var characterCode; // literal character code will be stored in this variable 
        if (e && e.which) { //if which property of event object is supported (NN4) 
            e = e; 
            characterCode = e.which; 
        } else { 
            characterCode = e.keyCode; 
        } 
        if (characterCode == 27)  //if esc key
        { 
            //do stuff 
        } 
    }); 
} 

它对我不起作用。但是,如果我要更改该行:

$('#txtSearch').bind('keyup', function(e) { 

与:

$(document).bind('keyup', function(e) { 

一切正常。但我不想将带有 esc 的 keyup 绑定到 整个文档...

关于如何将 keyup 仅与特定绑定的任何建议 文本搜索元素? (或者它包含的 div 什么的)

谢谢!

【问题讨论】:

  • 你所拥有的应该可以工作。您可以检查 $('#txtSearch').length 以查看该选择器是否匹配任何元素。
  • 您在使用 asp.net 吗?它会破坏元素 id,因此您可能希望使用一个类。

标签: jquery keyboard


【解决方案1】:

我认为

$('input#txtSearch').bind('keyup', function(e) {

是您要查找的线路。

善良,

【讨论】:

    【解决方案2】:

    确保您的 html 中有一个 id 为 txtSearch 的元素。

    如果您使用的是 ASP.NET,则必须更改

    $('#txtSearch').bind('keyup', function(e) { 
    

    $('#<%= txtSearch.ClientID %>').bind('keyup', function(e) { 
    

    以便 jQuery 获取完整生成的 id。

    【讨论】:

      【解决方案3】:

      或者如何使用:

      $('input[id=txtSearch]').bind('keyup', function(e) {
      

      只是一种解决方法。

      【讨论】:

        【解决方案4】:

        在调试器中检查$('#txtSearch').length 或键入

        javascript:alert($('#txtSearch').length); 
        

        到您的地址栏中,以确保它实际上是在选择一个元素。如果没有,您可以尝试在文本框中添加一个 CSS 类并使用$('.txtSearch')

        【讨论】:

          【解决方案5】:

          这对我有用:

          $(function(){ 
              $('input').keydown(function(e){ 
                  if (e.keyCode == 13) { 
                      $(this).parents('form').submit(); 
                       return false; 
                  } 
              }); 
          });  
          

          【讨论】:

            【解决方案6】:

            我放弃了。我似乎无法找到一种方法将此事件仅附加到 DOM 的特定元素。

            所以,这是我的工作:

            $(document).keyup(function(e) {
                if (e.target.id == "txtSearch") {
                    var characterCode; // literal character code will be stored in this variable
                    if (e && e.which) { //if which property of event object is supported (NN4)
                        e = e;
                        characterCode = e.which; //character code is contained in NN4's which property
                    }
                    else {
                        characterCode = e.keyCode; //character code is contained in IE's keyCode property
                    }
            
                    if (characterCode == 27)  //if generated character code is equal to ascii 27 (if esc key)
                    {
                       //do stuff
                    }
            
                }
            });
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-04-03
              • 2014-03-03
              • 2018-02-02
              • 1970-01-01
              • 2014-06-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多