Jquery 使用向上键和向下键在表格的文本框中设置值

Jquery 使用向上键和向下键在表格的文本框中设置值,jquery,html,netbeans,Jquery,Html,Netbeans,我已经创建了一个表,并使用其内容在文本框中设置值。我可以用鼠标点击来完成,也可以用键盘上下键在表格行上下移动,但我不能在文本框中设置值 这是我的密码 $(window).keyup(function (evt) { if (evt.keyCode == 38) { // up $('#autotable tr:not(:first).selected') .removeClass('selected').prev() .addClass('selected')

我已经创建了一个表,并使用其内容在文本框中设置值。我可以用鼠标点击来完成,也可以用键盘上下键在表格行上下移动,但我不能在文本框中设置值

这是我的密码

$(window).keyup(function (evt) {
  if (evt.keyCode == 38) { // up
    $('#autotable tr:not(:first).selected')
      .removeClass('selected').prev()
      .addClass('selected');
    $("#textp")
      .val($(this)
      .find('td:first').html()); //this line is not working
  }
  if (evt.keyCode == 40) { // down
    $('#autotable tr:not(:last).selected')
      .removeClass('selected').next()
      .addClass('selected');
    $("#textp").val($(this).find('td:first').html());
  }
});
下面是我的代码的第一部分,我使用ajax动态创建了表

   <SCRIPT language="JavaScript" type="text/javascript">
     var xmlHttpRequest=new XMLHttpRequest();
     function edValueKeyPress()
     {
         if(document.getElementById("textp").value!==""){
      xmlHttpRequest.open("get","/WebApplication1/NewServlet?id=" + document.getElementById("textp").value,true);
     xmlHttpRequest.onreadystatechange=process;
     xmlHttpRequest.send();
         }
         else
             {
                 var table=document.getElementById("autotable");
                 table.innerHTML="";
             }
     }

     function process()
     {

                 var JSONtopicobject=eval( "("+xmlHttpRequest.responseText + ")" );
                 console.log(JSONtopicobject);
                 var table=document.getElementById("autotable");
                 table.innerHTML="";

                 var headrow=table.insertRow(0);
                 var headcell=headrow.insertCell(0);
                 headcell.innerHTML=JSONtopicobject.topic.name;
                 var tutorials=JSONtopicobject.topic.tutorial;
                 var i=0;
                 while(i<tutorials.length)
                     {
                         row=table.insertRow(i);
                         row.className=JSONtopicobject.topic.name;
                         cell=row.insertCell(0);
                         var p=tutorials[i];
                         cell.innerHTML=p.name;
                         ++i;
                     }

     }
          </SCRIPT>
                <style type="text/css">
                        td {  cursor: pointer;}

.selected {
background-color: brown;
color: white;
}

  </style>





</head>

<body >
    <h1 style="background-image:url(320px-Draft01_wkp.png);font-size:400%;color: white;">GetaDictionary.com</h1>
    <form action="/WebApplication1/NewServlet" method="post">
        <input type="text" name="textp" id="textp" value=""  onkeypress="edValueKeyPress();" onkeydown="edValueKeyPress();" onkeyup="edValueKeyPress();"  />
        <input type="submit" name="button1" id="button1" value="SEARCH" />

        <br><table id="autotable" style="border: 1px solid grey;font-size:100%;border-top-color: white;" width="173.5" ></table>
    </form>


</body>

var xmlHttpRequest=新的xmlHttpRequest();
函数edValueKeyPress()
{
if(document.getElementById(“textp”).value!=“”){
xmlHttpRequest.open(“get”,“/WebApplication1/NewServlet?id=“+document.getElementById(“textp”).value,true);
xmlHttpRequest.onreadystatechange=进程;
xmlHttpRequest.send();
}
其他的
{
var table=document.getElementById(“自动表”);
表.innerHTML=“”;
}
}
函数过程()
{
var JSONtopicobject=eval(“+xmlHttpRequest.responseText+”);
log(JSONtopicobject);
var table=document.getElementById(“自动表”);
表.innerHTML=“”;
var水头=表.插入行(0);
var headcell=头流。插入单元(0);
headcell.innerHTML=JSONtopicobject.topic.name;
var tutorials=JSONtopicobject.topic.tutorial;
var i=0;
(i)请单击
按钮并创建一个,请单击
按钮并创建一个