【发布时间】:2018-07-25 03:37:24
【问题描述】:
我创建了自己的简单 jquery 插件,但是当我放置 2 个元素并在每个元素上调用插件时遇到了一些问题
我的js代码:
function fTable(element,options){
self = this;
this.$element = $(element);
this.table = $(this.$element).find('table');
this.thead = $(this.table).find('thead');
this.tbody = $(this.table).find('tbody');
coloumn = options.coloumn;
this.defaults = {
}
//Merge default options with user options
this.options = $.extend(true, {}, this.defaults, options);
this.init();
}
fTable.prototype = {
init : function(){
self = this;
this.td = $(this.thead).find('tr td:first');
$(this.td).html('<a class="add">Plus</a>');
this.bindEvents();
},
bindEvents : function(){
self = this;
console.log(this.table);
$(this.table).on('click', '.add', function(){
$row = '<tr>';
$row += '<td></td>';
$.each(coloumn, function(index, value){
$row += '<td><input type="text" value="" name='+value.name+' '+value.prop+'></td>';
});
$row += '</tr>';
console.log(self);
$($row).appendTo(self.table);
lastTR = $(self.tbody).find('tr:last');
$(lastTR).find('td:first').html('<a class="remove">Remove</a>');
});
$(this.table).on('click', '.remove', function(){
row = $(this).closest('tr');
$(row).remove();
});
}
}
$.fn.fTable = function(options){
return this.each(function(){
new fTable(this,options);
});
}
$('.crud').fTable({
coloumn:[
{'type':'text','name':'NIK','prop':'disabled'},
{'type':'text','name':'NAME','prop':''},
]
});
$('.crud2').fTable({
coloumn:[
{'type':'text','name':'NIK','prop':'disabled'},
{'type':'text','name':'NAME','prop':''},
]
});
HTML:
<div class="crud">
<table class="table table-bordered">
<thead>
<tr>
<td style="width:10%"></td>
<td>NIK</td>
<td>Name</td>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>1</td>
<td>Ardhi</td>
</tr>
<tr>
<td></td>
<td>2</td>
<td>Mega</td>
</tr>
</tbody>
</table>
</div>
<div class="crud2">
<table class="table table-bordered">
<thead>
<tr>
<td style="width:10%"></td>
<td>NIK</td>
<td>Name</td>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>1</td>
<td>Zaphire</td>
</tr>
<tr>
<td></td>
<td>2</td>
<td>Rexa</td>
</tr>
</tbody>
</table>
</div>
问题是当我单击第一个表(class='crud')上的“加号”时,它会将新行添加到第二个表(class='crud2')而不是第一个表(class='crud')
有什么帮助吗?
【问题讨论】:
标签: javascript jquery jquery-plugins