无法从列表框中按Enter键获取选定文本

时间:2013-06-06 05:33:13

标签: javascript jquery

我正在尝试在我的网页上进行智能搜索,所以在我对Click事件的搜索中我得到的文本在同一个textarea中被选中但是当我尝试使用On keypress时我无法找到所选项目的文本。我正在使用jquery1.4.2并尝试选择:选择但仍然无法这样做... 我的代码点击

$(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");
    });
  });
});  

请提前建议并提前致谢

1 个答案:

答案 0 :(得分:0)

嘿,我得到了答案,我在aspx页面上的文本框中调用了按键事件,从那里我进入了当前paosted js页面,然后在keypress事件上我得到了value.ie无法使用keyup事件来检测输入< / p>

相关问题