我对动态添加字段combaine MaskMoney有一些问题,
HTML部分
<div class='table-responsive mailbox-messages'>
<table id="dataTable" class='table table-hover'>
<tr>
<th>Keperluan</th>
<th>Tanggal Transaksi</th>
<th>Total Transaksi</th>
<th>Keterangan</th>
<th>Scan Foto</th>
<th>Action</th>
</tr>
<tbody class="input_fields_wrap">
<tr>
<td><input type="text" name="mytext[]" id="perlu'+x+'" class="form-control" required="required" placeholder="Keperluan"/></td>
<td><input type="text" name="mytexts[]" id="s" class="form-control tgl" required="required" placeholder="Tanggal Transaksi"/></td>
<td><input type="text" name="mytexts[]" id="demo1" class="form-control numeric " required="required" placeholder="Only Number"/></td>
<td><input type="text" name="mytexts[]" id="s" class="form-control" required="required" placeholder="Keterangan"/></td>
<td><input type="file" name="mytexts[]" id="s" class="small" required="required"/></td>
<td><button class="add_field_button btn btn-success">Add Field</button></td>
</tr>
</tbody>
</table>
</div>
的Javascript
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){
x++; //text box increment
i = $('<tr><td><input type="text" name="mytext[]" id="perlu'+x+'" class="form-control" required="required" placeholder="Keperluan"/></td><td><input type="text" name="mytexts[]" id="s" class="form-control tgl" required="required" placeholder="Tanggal Transaksi"/></td><td><input type="text" name="mytexts[]" id="demo'+x+'" class="form-control numeric" required="required" placeholder="Only Number" /></td><td><input type="text" name="mytexts[]" id="s" class="form-control" required="required" placeholder="Keterangan"/></td><td><input type="file" name="mytexts[]" id="s" class="small" required="required"/></td><td><a href="#" class="remove_field btn btn-danger">Remove</a></td></tr>');
//re-init mask money to apply new added input
$(wrapper).append(i); //add input box
}
});
$(wrapper).on('click', '.remove_field', function(e){ //Once remove button is clicked
e.preventDefault();
$(this).closest("tr").remove(); //Remove field html
x--; //Decrement field counter
});
});
$("#demo1").maskMoney({prefix:'Rp ', allowNegative: true, thousands:'.', decimal:',', affixesStay: false});
我的代码的运行代码段可以找到here
Field“Total Transaksi”,MaskMoney Notworking,
的问题第一个字段工作正常,但是当按钮添加新字段= NOT WORK。
我在互联网上搜索过,但答案仍然无法解决。
由于
答案 0 :(得分:1)
运行maskMoney()
方法时,只运行一次;在页面加载开始时。之后,其他新字段没有maskMoney()
提供的绑定。您需要重新绑定到每个新元素集或使用event delegation。它看起来不像maskMoney
插件具有委托功能,因此您可以重新创建每个按钮: