douyou7797 2017-04-07 06:03
浏览 100
已采纳

如何通过遍历父div来获取元素的值

I am working on a code where i have multiple div's generated dynamically. My problem is to get current hidden elements value on click of a link to that particular div(e.g when i click on Add to Favorites it should alert the value of task_for hidden input). My code is working fine with one div but if there is multiple divs it alerts the first element's value. Here is my code-

<div class="col-md-4 col-sm-4 col-xs-12">
<div class="search_box_list">
<a href="javascript:void(0)" class="add_fav"><i class="fa fa-star-o" aria-hidden="true"></i>Add to favourites</a>
<div class="n_s">'+value.user_name+'</div><div class="n_e">'+value.user_email+'</div>
<div class="n_n">'+value.user_phone+'</div>
<div class="n_d">'+value.user_designation+'</div><div class="n_d">
<button type="button" id="assign" class="assign_task" class="btn btn-default">
<input type="hidden" name="for_id" class="task_for" value="'+value.user_id+'" />
<input type="hidden" name="for_id" class="email" value="'+value.user_email+'" /> Assign Task
</button>
</div>
</div>
</div>

and the jquery

$("body").on('click','.add_fav',function(){
  var who_id=<?php echo $_SESSION['global']['uid'] ?>;
  var whom_id=$(document).find(".task_for").val();
  alert(whom_id)
});
  • 写回答

1条回答 默认 最新

  • duanbu4962 2017-04-07 06:06
    关注

    Use closest to get the parent .search_box_list and then use find to get the value of the hidden div

    $("body").on('click','.add_fav',function(){
      var who_id  = <?php echo $_SESSION['global']['uid'] ?>;
      var whom_id = $(this).closest('.search_box_list').find(".task_for").val();
      alert(whom_id)
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog