gpt4 book ai didi

javascript - 如何选择 50 个输入并添加功能

转载 作者:行者123 更新时间:2023-12-02 14:33:40 24 4
gpt4 key购买 nike

我有 50 个输入元素,只允许使用数字:

function number_format_input(s) {
var tmp;
tmp = parseFloat(jQuery(s).val());
if (tmp) {
jQuery(s).val(tmp.toFixed(0));
} else {
jQuery(s).val("0");
}
}

jQuery("#m15710").blur(function () { number_format_input("#m15710"); });
/* ... 50x... */
jQuery("#m20390").blur(function () { number_format_input("#m20390"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<input class="edit_plan" type="text" id="m15710" name="data[15710][menge]" value="0">
<!-- ... 50x ... -->
<input class="edit_plan" type="text" id="m20390" name="data[20390][menge]" value="0">

可以免写50次

jQuery("#xxx").blur( function(){ number_format_input("#xxx"); } );

最佳答案

您可以使用 .edit_plan 类代替 id,如下所示。

function number_format_input(s) {
var tmp;
tmp = parseFloat(jQuery(s).val());
if (tmp) {
jQuery(s).val(tmp.toFixed(0));
} else {
jQuery(s).val("0");
}
}

jQuery(".edit_plan").blur(function () {
number_format_input(this);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="edit_plan" type="text" id="m15710" name="data[15710][menge]" value="0">
<input class="edit_plan" type="text" id="m20390" name="data[20390][menge]" value="0">

关于javascript - 如何选择 50 个输入并添加功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37633272/

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