简体   繁体   English

最近发现无法正常工作的jQuery

[英]Closest and find not working jquery

I have a Bootstrap Tab and a Table within the Tab and within the Table, a row with input fields. 我有一个Bootstrap选项卡,并且该选项卡内和表内都有一个表,其中包含输入字段。 The problem I am experiencing is when I have multiple rows within the table only the first row values get pulled. 我遇到的问题是当表中有多行时,只有第一行值被提取。 I am using 'closet' and 'find' but I'm not getting the desired values. 我正在使用“壁橱”和“查找”,但没有得到所需的值。 What am I doing wrong? 我究竟做错了什么?

<ul class="nav nav-tabs nav-justified itemTab">
<li><a href="#tab5" data-toggle="tab">Inbox</a></li> 
<li><a href="#tab6" data-toggle="tab">Outbox</a></li>
</ul>
<div class="tab-content itemTab">
<div class="tab-pane fade in" id="tab5">
    <table class="table table-condensed" id="utable">
        <th>Location</th>
        <th>Department</th>
        <th>HD Number</th>
    @foreach (var item in Model.items)
    {
        <tr class="@item.Id">
        <div class="accordian-body collapse in" id="@item.Id">
            <input type="text" class="form-control" id="loc">
            <input type="text" class="form-control" id="dept">
            <input type="text" class="form-control" id="hdnum">
        </div>
         <div class="row">
            <div align="center">
            <input type="submit" value="Transfer" class="btn btn-success" onclick="MoveItem(@item.Id)" id="moveButton" data-url="@Url.Action("Submit", "Item")" />
            </div>
        </div>
        </tr>
    }
    </table>
    </div>
    <div class="tab-pane fade in" id="tab6">
    @Html.Action("Outbox", "Item") 
    </div>


   function MoveItem(id) {
   var row = $('#' + id).closest('.itemTab');
   var loc = $(row).find('#loc').val();
   var dept = $(row).find('#dept').val();
   var hdnum = $(row).find('#hdnum').val();
   }

First, wrap the id value in quotes when generating parameter for MoveItem call: 首先,在为MoveItem调用生成参数时,将id值用引号MoveItem来:

onclick="MoveItem('@item.Id')"

Second, change your repeating id's to classes and go up only as far as the parent tr : 其次,将重复的ID更改为class,并且仅向上移动至父级tr

<input type="text" class="form-control" class="loc">

function MoveItem(id) {
  var row = $('#' + id).closest('tr');
  var loc = $(row).find('.loc').val();
}

Your closest 最近的

var row = $('#' + id).closest('.itemTab');

gets the tab - but all of the "rows" are in that tab. 获取选项卡-但所有“行”都在该选项卡中。

Instead, get the "row" 相反,获取“行”

var row = $('#' + id).closest('tr'); 

then the finds will find the item in the relevant row. 然后发现将在相关行中找到该项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM