首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从列表框按Enter键时无法获取所选文本

问从列表框按Enter键时无法获取所选文本
EN

Stack Overflow用户
提问于 2013-06-06 13:33:13
回答 1查看 788关注 0票数 0

我正在尝试在我的网页上进行智能搜索,所以在我的search on Click事件中,我得到了在相同文本区域中选择的文本,但当我尝试使用on keypress时,我无法找到所选项目的文本。我使用的是jquery1.4.2,并尝试了option:select,但仍然无法这样做……我的点击代码

代码语言:javascript
复制
$(document).ready(function() {

  $('#ctl00_ContentPlaceHolder1_smartSearch').keyup(function(e) {
    var str = document.getElementById('ctl00_ContentPlaceHolder1_smartSearch');
    //alert("hi");

    if (str.value.length >= 2) {

      if (e.keyCode != 40 && e.keyCode != 38 && e.keyCode != 13) {
        var str1 = str.value;
        var str2 = str1.replace(' ', '+')
        var url = "../SiteCache/90D/SmartGetQuoteData.aspx?Type=EQ&text=" + str2;

        $("#ajax_response_smart").load(url);

      }

      $("#ajax_response_smart").show();
      $('#listEQ li').live('click', function() {        
        var selected = $(this).text();              
        $("#ajax_response_smart").remove(); 
        if (selected != "") {
          var scripcode = selected.split("|");
          document.getElementById('ctl00_ContentPlaceHolder1_smartSearch').value = scripcode[0];
          document.getElementById('ctl00_ContentPlaceHolder1_hdnCode').value = scripcode[2];                    
        }
      });

      var $listItems = $('#listEQ li');
      var key = e.keyCode,
      $selected = $listItems.filter('.ui-state-hover'),
            $current;

      if (key != 40 && key != 38 && key != 13)
      { return; }
      else {
        $listItems.removeClass('ui-state-hover');
      }


      if (key == 40) // Down key
      {
        if (!$selected.length || $selected.is(':last-child')) {
          $current = $listItems.eq(0);
        }
        else {

          $current = $selected.next();
        }
      }
      else if (key == 38) // Up key
      {
        if (!$selected.length || $selected.is(':first-child')) {
          $current = $listItems.last();
        }
        else {
          $current = $selected.prev();
        }
      }
      else if (key == 13) {
      }
    }
    else {
      $("#ajax_response_smart").hide();
    }
    if (typeof $current != "undefined") {
      $current.addClass('ui-state-hover');
    }
  });
  $("#ajax_response_smart").mouseover(function() {
    var $listItems1 = $('#listEQ li');
    $selected1 = $listItems1.filter('.ui-state-hover');
    $(this).find("#listEQ li").mouseover(function() {
      ($selected1).removeClass("ui-state-hover");
      $(this).addClass("ui-state-hover");
    });
    $(this).find("#listEQ li").mouseout(function() {
      $(this).removeClass("ui-state-hover");
    });
  });
});  

请提前提出建议并致谢

EN

回答 1

Stack Overflow用户

发布于 2013-06-07 20:16:26

嘿,我得到了我的答案,我在aspx页面的文本框上调用了keypress事件,从那里我进入了当前paosted的js页面,然后在keypress事件上,我得到了值。即不能使用keyup事件来检测回车

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16954345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档