gpt4 book ai didi

javascript - 掩码货币不适用于动态添加的输入

转载 作者:行者123 更新时间:2023-12-02 15:58:09 38 4
gpt4 key购买 nike

我正在使用jquery.maskMoney.js向输入添加掩码,它适用于正常输入。但它不适用于动态添加的输入。这是我正在使用的代码

$(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
$(wrapper).append('<div><input type="text" name="mytext[]" id="demo['+x+']" /><a href="#" class="remove_field">Remove</a></div>'); //add input box
}
});
$('input[id^="demo"]').each(function () {
$(this).maskMoney({prefix:'R$ ', thousands:'.', decimal:',', affixesStay: true});
});


<div class="input_fields_wrap">
<button class="add_field_button">Add More Fields</button>
<div><input type="text" name="mytext[]" id="demo[1]"></div>
</div>

请帮助我,我被困住了

最佳答案

你可以这样改变你的javascript。请参阅 initMaskMoney(),它在页面加载后和添加文本字段后都会调用。此外,您不需要迭代每个元素,您可以使用选择器一次选择所有元素。

<script type="text/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
$(wrapper).append('<div><input type="text" name="mytext[]" id="demo['+x+']" /><a href="#" class="remove_field">Remove</a></div>');
//re-init mask money to apply new added input
initMaskMoney();
}
});
initMaskMoney();

});

function initMaskMoney() {
$('input[id^="demo"]').maskMoney({prefix:'R$ ', thousands:'.', decimal:',', affixesStay: true});
}

</script>

更新看到@Dekel的回答后我认为这个版本更方便。 initMaskMoney 获取一个名为选择器的参数。借助此参数,您可以为当前项目和仅新添加的输入初始化 maskmoney(在前面的答案中,所有输入框都再次初始化 MaskMoney)。

<script type="text/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
var newItem = $('<div><input type="text" name="mytext[]" id="demo['+x+']" /><a href="#" class="remove_field">Remove</a></div>')
$(wrapper).append(newItem); //add input box
//init mask money for only added input
initMaskMoney($(newItem).find('input'));
}
});

//init mask money for current inputs
initMaskMoney('input[id^="demo"]');

});

function initMaskMoney(selector) {
$(selector).maskMoney({prefix:'R$ ', thousands:'.', decimal:',', affixesStay: true});
}

</script>

关于javascript - 掩码货币不适用于动态添加的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31426733/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com