gpt4 book ai didi

javascript - 带有选择器作为参数的 jQuery 函数

转载 作者:行者123 更新时间:2023-11-30 20:24:24 25 4
gpt4 key购买 nike

我正在尝试创建一个将当前元素的值自动添加到另一个选择器中的函数,如下所示:

function calculate(current,selector) {
var sum = $(selector).val();
if (!isNaN($(current).value) && $(current).value.length != 0) {
sum += parseFloat($(current).value);
}
$(selector).val(sum.toFixed(2));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="one" onkeyup="calculate(this,'#three')" >
<input type="text" id="two" onkeyup="calculate(this,'#five')" >
<input type="text" id="three">
<input type="text" id="five">

最佳答案

你正在传递给 onkeyup 已经执行的函数,所以基本上你传递的是 undefined 作为将被调用的东西。尝试:

<input type="text" id="two" onkeyup="() => calculate(this,'#five')" >

关于javascript - 带有选择器作为参数的 jQuery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51079709/

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