Jquery 按编辑按钮时填充输入字段
我正在寻找一种通过jQuery/Javascript用我从表中获取的数据填充一些输入字段的方法。我的表格如下所示:Jquery 按编辑按钮时填充输入字段,jquery,dom,input,html-table,Jquery,Dom,Input,Html Table,我正在寻找一种通过jQuery/Javascript用我从表中获取的数据填充一些输入字段的方法。我的表格如下所示: <tr> <th>ticketID</th> <th>eventID</th> <th>name</th> <th>price</th> <th>pri
<tr>
<th>ticketID</th>
<th>eventID</th>
<th>name</th>
<th>price</th>
<th>priceWithinAllocation</th>
<th> </th>
</tr>
<tr >
<td class="ticketID">1</td>
<td class="eventID">1</td>
<td class="name">Sun</td>
<td class="price">300</td>
td class="priceWithinAllocation">150</td>
<td align="center"><input type="button" class="editRow" value="EDIT"/></td>
</tr>
<tr >
<td class="ticketID">2</td>
<td class="eventID">1</td>
<td class="name">Mon</td>
<td class="price">300</td>
<td class="priceWithinAllocation">150</td>
<td align="center"><input type="button" class="editRow" value="EDIT"/></td>
</tr>
试试这个:
$(function(){
$(".editRow").click(function(){
var name = $(this).parent().siblings(".name").text();
var price = $(this).parent().siblings(".price").text();
var priceWithinAllocation = $(this).parent().siblings(".priceWithinAllocation").text();
$("[name='name']").val(name);
$("[name='price']").val(price);
$("[name='priceWithinAllocation']").val(priceWithinAllocation);
});
});
工作示例:
备选方案:
$(function(){
$(".editRow").click(function(){
$("[name='name']").val($(this).parent().siblings(".name").text());
$("[name='price']").val($(this).parent().siblings(".price").text());
$("[name='priceWithinAllocation']").val($(this).parent().siblings(".priceWithinAllocation").text());
});
})
当然有可能
$('.editRow').click(function(){
var $row = $(this).closest('tr');
$('input[name="name"]').val($('.name',$row).text());
$('input[name="price"]').val($('.price',$row).text());
$('input[name="priceWithinAllocation"]').val($('.priceWithinAllocation',$row).text());
})
实例:
$(function(){
$(".editRow").click(function(){
$("[name='name']").val($(this).parent().siblings(".name").text());
$("[name='price']").val($(this).parent().siblings(".price").text());
$("[name='priceWithinAllocation']").val($(this).parent().siblings(".priceWithinAllocation").text());
});
})
$('.editRow').click(function(){
var $row = $(this).closest('tr');
$('input[name="name"]').val($('.name',$row).text());
$('input[name="price"]').val($('.price',$row).text());
$('input[name="priceWithinAllocation"]').val($('.priceWithinAllocation',$row).text());
})