【发布时间】:2015-07-14 18:01:29
【问题描述】:
总结: 我有一个 html 页面,其中包含两个添加按钮和两个表格。单击添加按钮时,行将附加到相应的表中。我在一个 html 模板中包含了两个具有两个不同父 ID 的模板。我需要以这样一种方式编写脚本,即当单击具有 parentid ==“#a”的 add-btn 时,将行附加到具有 parentid =="#a" 的表中。
结果.html
//i am using django web framework
<div class="reports">
{% include 'template1.html' with id="a" %}
{% include 'template2.html' with id="b" %}
</div>
每个模板共享/扩展一个 base.html 模板,所有通用代码都在其中编写
<div class="panel">
<div>
<button type="button" class="add btn btn-primary" data-toggle="tooltip" title="Add to configuration list.">
<i class="fa fa-plus"></i>
Add
</button>
</div>
<div class ="configuration-table panel-body">
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th>Remove</th>
<th>Layer</th>
<th>Display</th>
<th>Unit</th>
<th>Dataset</th>
<th>Ordering</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
我的 jquery 代码是
result.js
$('.reports').on('click','#a .add, #b .add', function(e) {
//how to differentiate two tables here.
$(this).find('configuration-table table').children('tbody')
.append(
'<tr>'+
'<td class="remove-row" role="button" aria-label="Remove Region"><i class="fa fa-times" title="Remove this row." data-toggle="tooltip" data-placement="right" aria-hidden="true"></i></td>'+
'<td>'+layer_text+'</td>'+
map_elements+
'<td>'+unit_text+'</td>'+
'<td>'+dataset_text+'</td>'+
'<td class="ordering" aria-label="Re-order"><i class="fa fa-arrows" title="Re-arrange row order." data-toggle="tooltip" data-placement="right"></i></td>'+
'</tr>'
);
问题:当我单击添加按钮时,该行将附加到两个表中。我不想要那个。我想将行添加到相应的表中。我想在同一个函数中做到这一点。
我正在寻找我在这里缺少的逻辑。
提前致谢
【问题讨论】:
-
一个fiddle 会很棒!
-
.on(与什么有关?你正在寻找一个元素而不是一个类。不确定该代码将如何工作,因为该表不是按钮的子项。 -
@epascarello 我在这里寻找逻辑。请避免指出代码中的愚蠢错误。我的要求是理解这个概念。反正我已经编辑过了。
-
愚蠢的错误让人很难理解发生了什么。有时,“愚蠢”的错误是人们的代码不起作用的原因。请记住,我们正在努力帮助您。
标签: javascript jquery