求a 并将其检索到输入框中

本文关键字:检索 输入 TD | 更新日期: 2023-09-27 18:08:50

我有一些麻烦得到文本在a和把它放在一个框,在这里你可以找到一个小的例子。这个想法是当用户点击"选择"时它会把代码从

<td> @Html.DisplayFor(Mod => Item.Item.Custumer_code) <td/>

中的Input输入带有customercode ID。下面是代码:

<div class="col-sm-3">
<input class="form-control"
       placeholder="Entre a custumer code"
       id="Custumercode" 
       name="Custumercode"/>
</div>
<table class="table">
   <thead>
    <tr>
        <th>Costumer Code</th>
        <th>Select</th>
        <th>Costumer Name</th>
        <th>Email </th>
        <th>Tel </th>
    </tr>
</thead>
@foreach (var Item in Model)
{
<tbody>
    <tr>
        <td> @Html.DisplayFor(Mod => Item.Custumer_code) </td>
        <td> @Html.DisplayFor(Mod => Item.custumer_name) </td>
        <td> @Html.DisplayFor(Mod => Item.Email) </td>
        <td> @Html.DisplayFor(Mod => Item.Tel) </td>
        <td>
            <button type="button" class="btn" onclick="myFunction()">
                Select
            </button>
        </td>
    </tr>
</tbody>
}
</table>`

求a <TD>并将其检索到输入框中

试试这个

$(document).ready(function(){
$(document).on('click','.btn',function(){
 var customer = $(this).parents('tr').find('td:eq(0)').text();
 $('#Custumercode').val(customer );
});
});
演示

试试这个:

$(document).ready(function(){
   $('.btn').click(function(){
     var customerCode = $(this).closest('tr').find('td:nth-child(1)').text();
     $('#Custumercode').val(customerCode );
    });
});