[英]get attribute value of div in jquery
I have a table in div and want to get data-diet_for of parent div where the class is row by jquery我在 div 中有一个表,想获取父 div 的data-diet_for ,其中该类是由 jquery行的
function table2json() { var details = []; $('.myTable tbody tr').each(function(i, e) { details[i] = { diet_for_id: $(this).closest('.row').attr('data-diet_for') }; }); return(details); } console.log(table2json);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card">
<div class="card-body">
<div class="card-title toggle d-flex justify-content-between" data-toggle="collapse" data-target="#breakfast">
<strong>Breakfast <span id="breakfast_time"></span></strong>
<span class="btn btn-sm">
<i class="fas fa-fw fa-plus"></i>
</span>
</div>
<div class="row collapse" id="breakfast" data-diet_for="1">
<div class="col-md-12 table-responsive">
<table class="table myTable bg-white" style="width:100%" id="breakfast_table">
<thead class="thead-light">
<tr>
<th>Dish</th>
<th>Type</th>
<th>Quantity</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
.....
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
This gives me undefined
output while using $(this).closest('.card').find('.row').attr('data-diet_for')
gives absolute output then why first code is not working这给了我
undefined
输出,同时使用$(this).closest('.card').find('.row').attr('data-diet_for')
给出绝对输出然后为什么第一个代码不起作用
it works fine.它工作正常。
var details = []; $('.myTable tbody tr').each(function(i, e) { details[i] = { diet_for_id: $(this).closest('.row').attr('data-diet_for') }; }); console.log(details)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="card"> <div class="card-body"> <div class="card-title toggle d-flex justify-content-between" data-toggle="collapse" data-target="#breakfast"> <strong>Breakfast <span id="breakfast_time"></span></strong> <span class="btn btn-sm"> <i class="fas fa-fw fa-plus"></i> </span> </div> <div class="row collapse" id="breakfast" data-diet_for="1"> <div class="col-md-12 table-responsive"> <table class="table myTable bg-white" style="width:100%" id="breakfast_table"> <thead class="thead-light"> <tr> <th>Dish</th> <th>Type</th> <th>Quantity</th> <th>Action</th> </tr> </thead> <tbody> <tr> ..... </tr> </tbody> </table> </div> </div> </div> </div>
Your code is working您的代码正在运行
You should not return a value as you are not inside a function
.您不应返回值,因为您不在
function
。
You can log the value inside a console.log()
or do something else with it.您可以将值记录在
console.log()
或用它做其他事情。
var details = []; $('.myTable tbody tr').each(function(i, e) { details[i] = { diet_for_id: $(this).closest('.card').find('.row').attr('data-diet_for') }; }); console.log(details);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="card"> <div class="card-body"> <div class="card-title toggle d-flex justify-content-between" data-toggle="collapse" data-target="#breakfast"> <strong>Breakfast <span id="breakfast_time"></span></strong> <span class="btn btn-sm"> <i class="fas fa-fw fa-plus"></i> </span> </div> <div class="row collapse" id="breakfast" data-diet_for="1"> <div class="col-md-12 table-responsive"> <table class="table myTable bg-white" style="width:100%" id="breakfast_table"> <thead class="thead-light"> <tr> <th>Dish</th> <th>Type</th> <th>Quantity</th> <th>Action</th> </tr> </thead> <tbody> <tr> ..... </tr> </tbody> </table> </div> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.