gpt4 book ai didi

javascript - 当第二个值设置文本时,如何在文本字段上设置总和值?

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

这是我的演示线。

<input type="text" id="my_input1" />
<input type="text" id="my_input2" />
<input type="text" id="total" />
<input type="button" value="Add Them Together" onclick="doMath();" />
<script type="text/javascript">
function doMath()
{
// Capture the entered values of two input boxes
var my_input1 = document.getElementById('my_input1').value;
var my_input2 = document.getElementById('my_input2').value;

// Add them together and display
var sum = parseFloat(my_input1) + parseFloat(my_input2);
document.getElementById('total').value=sum;
}


我想在 my_input2 输入它的值时使用这个函数。就像按钮的 onclick 方法一样,在按键释放事件之后是否有任何事件可以将值设置为总 tetxfeild ?

最佳答案

   <script type="text/javascript">
$(document).ready(function () {


$("#my_input2").blur(function () {
var sum = parseInt($("#my_input1").val()) + parseInt($("#my_input2").val());

$("#total").val(sum);

});


});

</script>


<div>
<input type="text" id="my_input1" />
<input type="text" id="my_input2" />
<input type="text" id="total" />
<input type="button" value="Add Them Together" />

</div>

而且你也应该正确地提出你的问题,因为你在按钮点击中添加了代码,并询问我们它应该在离开文本框后起作用

关于javascript - 当第二个值设置文本时,如何在文本字段上设置总和值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20454576/

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