如何通过单击另一个在同一不同​​

问题描述:

<tr class="item-row" id="itemsRow"> 
     <td></td> 
     <td><input class="tInput" id="invoiceNo" readonly/> </td> 
     <td><input class="tInput" id="clientId" readonly /> </td> 
     <td><input class="tInput" id="issueDate" readonly/></td> 
     <td><inpu class="tInput" id="netTotal" readonly /> </td> 
     <td><input class="tInput" id="amountDue" readonly /> </td> 
     <td><input class="tInput" id="status" readonly /> </td> 
</tr> 

我试着当我点击“输入ID =地位”得到的“输入ID = invoiceNo的价值得到的值使用下面的代码如何通过单击另一个<input>在同一<tr>不同​​

var invno = $(this).closest('tr td:nth-child(2) input').val(); 
alert(invno); 

It alerts'undefined'

那么如何解决这个问题?

+0

使用DOM遍历来查找具有ID(该页面应该是唯一的)的元素较慢,写入较复杂,并且对读取该代码的其他人来说稍微不明显。只需使用ID选择器。 – 2012-03-21 08:47:02

+0

犯错,我是唯一一个看到问题有多个ID相同 - 每个表中有一行? – frumbert 2012-03-21 09:10:05

closest只搜索parent()阵列。 所以,你必须在你的父元素搜索:

var invno = $(this).closest('tr').find('td:nth-child(2) input').val(); 

我会做

var invo = $(this).parents('tr').find('#invoiceNo').val() 

尝试使用此

$(this).closest('tr td:nth-child(1)').find('input#invoiceNo').val(); 

你有一个的ID 应该是独一无二的该页面(如果不是,则需要修复该页面),因此只需使用标准ID选择器($('#invoiceNo').val();)直接选择该元素即可。不需要进行DOM遍历。