简体   繁体   English

我们如何使用js在循环表中获取隐藏值?

[英]how can we use js to get hidden value in loop table?

I have the list and loop it in table, and I have table like this: 我有列表并在表中循环,而我有这样的表:

<table class="table table-striped">
 <tr>
   <td>Hidenfield</td>
   <td>Dropdownlist</td>
 </tr>
//loop here and have result
  <tr>
    <td><input type='hiden' value='1' class='myclass'/></td>
    <td>
       <select id="cateActive" class="activeStatus" name="cateActive">
        <option selected="selected" value="1">Active</option>
        <option value="0">Deactive</option>
       </select>
     </td>
  </tr>
  <tr>
    <td><input type='hiden' value='2' class='myclass'/></td>
    <td>
       <select id="cateActive" class="activeStatus" name="cateActive">
        <option selected="selected" value="1">Active</option>
        <option value="0">Deactive</option>
       </select>
     </td>
  </tr>
  <tr>
    <td><input type='hiden' value='3' class='myclass'/></td>
    <td>
       <select id="cateActive" class="activeStatus" name="cateActive">
        <option selected="selected" value="1">Active</option>
        <option value="0">Deactive</option>
       </select>
     </td>
  </tr>
</table>

and I have a jquery function like this: 而且我有一个jQuery函数是这样的:

$(document).ready(function(){       
    $(".activeStatus").change(function(){

         var categoryId = $('.myclass').val();
         alert(categoryId);

 });
});

when I click in any dropdownlist it has show the value is 1, how can I action on any dropdownbox to get hidden value in row? 当我在任何下拉列表中单击时,其显示的值为1,如何对任何下拉框进行操作以获取行中的隐藏值?

Try this code: 试试这个代码:

$(document).ready(function(){       
    $(".activeStatus").change(function(){
        var categoryId = $(this).parent().prev().find('.myclass').val();
        alert(categoryId);
    });
});

You can use combination of .closest() and .find() 您可以结合使用.closest().find()

var categoryId = $(this).closest('tr').find('.myclass').val();

Fiddle DEMO 小提琴演示

Here is working demo 这是工作演示

use delegate as you adding rows on run time and select particular row element: 在运行时添加行时使用委托,并选择特定的行元素:

$(document).ready(function(){       
  $(".activeStatus").on('change',function(){    
      var categoryId = $(this).closest('tr').find('.myclass').val();
      alert(categoryId);    
  });
});

Because $('.myclass') returns an array with more than one elements and the plain call of .val() returns always the value from first elment. 因为$('.myclass')返回一个包含多个元素的数组,而.val()的普通调用始终返回第一个元素的值。 So you have to iterate throught the array -here categoryId is your result array (or list). 因此,您必须遍历数组-这里categoryId是您的结果数组(或列表)。

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

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