【问题标题】:Find the value of a <TD> and retrive it into input box查找 <TD> 的值并将其检索到输入框中
【发布时间】:2014-06-16 15:50:44
【问题描述】:

我在将文本放入 a 并将其放入 Box 时遇到了一些麻烦,在这里你可以找到一个小例子。 这个想法是当用户单击“选择”时,它会将代码从内部

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

在带有客户代码 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>`

【问题讨论】:

    标签: c# jquery html asp.net-mvc-4


    【解决方案1】:

    试试这个

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

    DEMO

    【讨论】:

    • 您复制了我的答案,因为您的代码中不存在变量 customerCode :)
    • 是的,这很好用,非常感谢您
    【解决方案2】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多