在不使用jquery的表中追加新行后设置焦点

时间:2022-10-05 09:49:06

i have write this code

我写了这段代码

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

the HTML code

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>

this the button on even click :

这个按钮甚至点击:

<button onclick="addNew()"></button>

but nothing happened,can anyone help me

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

2 个解决方案

#1


2  

I think you need to find input inside container which is tbody, also make sure that id of this tbody must be unique throughout the DOM. please see below code

我认为你需要在容器内找到tbody的输入,同时确保这个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();
}

#2


0  

first of all check to see if there are any console errors - they are usually prety descriptive of what is going on and you can fix them,

首先检查是否有任何控制台错误 - 它们通常是描述正在发生的事情,你可以修复它们,

secondly - this may be getting added and you just don't know it because you have an invalid input tag as the only thing in your row. try fixing it

其次 - 这可能会被添加,你只是不知道它,因为你的行中唯一的输入标记是无效的。试着修理它

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

also - to simplify things , I would of just wrote :

另外 - 为了简化事情,我只想写:

$('#container input').last();

and another thing , this is bad and confusing :

另一件事,这是糟糕和令人困惑的:

<table id="#myTable"> // -- take that # out of here

// - 把##从这里拿走

#1


2  

I think you need to find input inside container which is tbody, also make sure that id of this tbody must be unique throughout the DOM. please see below code

我认为你需要在容器内找到tbody的输入,同时确保这个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();
}

#2


0  

first of all check to see if there are any console errors - they are usually prety descriptive of what is going on and you can fix them,

首先检查是否有任何控制台错误 - 它们通常是描述正在发生的事情,你可以修复它们,

secondly - this may be getting added and you just don't know it because you have an invalid input tag as the only thing in your row. try fixing it

其次 - 这可能会被添加,你只是不知道它,因为你的行中唯一的输入标记是无效的。试着修理它

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

also - to simplify things , I would of just wrote :

另外 - 为了简化事情,我只想写:

$('#container input').last();

and another thing , this is bad and confusing :

另一件事,这是糟糕和令人困惑的:

<table id="#myTable"> // -- take that # out of here

// - 把##从这里拿走