我正在尝试创建一个将当前元素的值自动添加到另一个选择器中的函数,如下所示:
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/