获取多个输入类型隐藏值行按钮单击表中的列

时间:2017-02-12 09:16:09

标签: jquery asp.net-mvc

这是单行的示例列,我试图通过按钮操作事件获取隐藏值

<td colspan="2">
    <!-- the hidden value i want to retrieve "stockitemid" and "outletid"  -->
    <!-- value i have bind from controller through model like @item.ID= stockitemid and @item.OUTLET_ID=outletid -->
    <input type="hidden" class="itemid" name="stockitemid" value="@item.ID"/>  
    <input type="hidden" class="oid" name="outletid" value="@item.OUTLET_ID"/>
    <div class="btn-toolbar row-action">
        <div class="btn-group pull-right">
            <!-- button action given below what i have tried to display value in modal form  controller. but every-time i click same value found from this event -->
            <button class="btn btn-primary" id="btnitemaddtotray" data-toggle="modal" data-target="#addtotrayModal" title="Add Item"></button>
        </div>
    </div>
</td>

每当我得到相同的“stockitemid”和“outletid”时,我的检索jquery代码

<script>
    $('#btnitemaddtotray').click(function() {
       // this is what i have tried to get row index with column index value
       $('td').click(function () {
           var col = $(this).parent().children().index($(this));
           var row =    $(this).parent().parent().children().index($(this).parent());
            alert('Row: ' + row + ', Column: ' + col);
        });  
        // here is itemid and outlet id i got every-time same value. 
        var itemid = $('input[name=stockitemid]').val();
        var outletid =$('input[name=outletid]').val();
    });
</script> 

我想从表中获取隐藏值,其中我有多行。

1 个答案:

答案 0 :(得分:0)

ID应始终对每个元素都是唯一的,因此我使用的是类。使用parents()将为您提供find()隐藏输入的容器以检索值。

&#13;
&#13;
$('.btnitemaddtotray').on('click',function() {
  
  var btn = $(this),
      row = btn.parents('td').first();
      itemId = row.find('.itemid').val(),
      oid = row.find('.oid').val();
      
      console.log(itemId,oid);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table><tr>
<td colspan="2">
    <input type="hidden" class="itemid" name="stockitemid" value="@item.ID"/>  
    <input type="hidden" class="oid" name="outletid" value="@item.OUTLET_ID"/>
    <div class="btn-toolbar row-action">
    <div class="btn-group pull-right">
    <button class="btn btn-primary btnitemaddtotray" data-toggle="modal" data-target="#addtotrayModal" title="Add Item">Add Item</button>
    </div></div>
    </td></tr>
<tr>
<td colspan="2">
    <input type="hidden" class="itemid" name="stockitemid" value="@item.ID2"/>  
    <input type="hidden" class="oid" name="outletid" value="@item.OUTLET_ID2"/>
    <div class="btn-toolbar row-action">
    <div class="btn-group pull-right">
    <button class="btn btn-primary btnitemaddtotray" data-toggle="modal" data-target="#addtotrayModal" title="Add Item">Add Item</button>
    </div></div>
    </td></tr>
</table>
&#13;
&#13;
&#13;