[英]"autoNumeric.js" not working in dynamically created form element
I'm using AutoNumeric.js (v4.6.0) in my project and I have a form that dynamically creates a price field.我在我的项目中使用 AutoNumeric.js (v4.6.0),我有一个动态创建价格字段的表单。 When the page is loaded, the first input that comes by default works fine, but the ones that are created automatically afterwards do not work unfortunately.加载页面时,默认情况下的第一个输入可以正常工作,但不幸的是,之后自动创建的输入不起作用。 How can i solve this problem?我怎么解决这个问题? Thank you from now.从现在开始谢谢你。
$(function () {
new AutoNumeric(".autonumber", { currencySymbol : '$' });
$(document).on('click', '#addSpending', function () {
var newSpending =
'<div class="row g-1">' +
'<div class="col-md-12">' +
'<input type="text" class="form-control autonumber" name="amount[]" required>' +
'</div>' +
'</div>';
$('#spendings').append(newSpending);
});
});
You need to use the AutoNumeric
instance after you append the field to the form like so:在将字段 append 转换为如下形式后,您需要使用AutoNumeric
实例:
$(function () {
$(document).on("click", "#addSpending", function () {
var newSpending =
'<div class="row g-1">' +
'<div class="col-md-12">' +
'<input type="text" class="form-control autonumber" name="amount" required>' +
"</div>" +
"</div>";
$("#spendings").append(newSpending);
new AutoNumeric("input[name=amount]", {
currencySymbol: "$"
})
});
});
put inside your looping bro sist this code $('.auto').autoNumeric('init');将这段代码放入你的循环兄弟中 $('.auto').autoNumeric('init');
<script type="text/javascript"> jQuery(function($) { $('.auto').autoNumeric('init'); }); $(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){ //max input box allowed var newSpending = '<input type="text" class="form-control col-4 mr-2 auto text-right" name="topamount[]""/>'; x++; //text box increment $(wrapper).append(newSpending); //add input box jQuery(function($) { $('.auto').autoNumeric('init'); }); } }); $(wrapper).on("click",".remove_field", function(e){ //user click on remove text e.preventDefault(); $(this).parent('div').remove(); x--; }) }); </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.