在表中追加新行后设置焦点不适用于 jquery

use*_*930 1 javascript jquery

我写了这段代码

function addNew(){
$('#container').append(
'<tr class="records" height="1em">'
+'<td align="center"><input  id="" name="UraiF" ></td></tr>');
$('table#myTable tr:last td:first input').focus();}
Run Code Online (Sandbox Code Playgroud)

HTML 代码

<table id="#myTable">
<thead>
<th style="width:5%;" align="center">No.</th>
</thead>
<tbody id="container">
<tr class="records">
<td><input  id="" name="UraiF" ></td>
<tr>
</table>
Run Code Online (Sandbox Code Playgroud)

这是甚至单击的按钮:

<button onclick="addNew()"></button>
Run Code Online (Sandbox Code Playgroud)

但什么也没发生,任何人都可以帮助我

Bhu*_*kar 6

我认为你需要找到containertbody 中的输入,并确保该 tbody 的 id 在整个 DOM 中必须是唯一的。请看下面的代码

function addNew(){
  $('#container').append(
  '<tr class="records" height="1em">'
   +'<td align="center"><input  id="" name="UraiF" ></td></tr>');
  $('tbody#container tr:last td:first input').focus();
}
Run Code Online (Sandbox Code Playgroud)