我现在有三个文本框:
<html>
<body>
<form>
第一个数:<input type="text" name="first" id="first">
第二个数:<input type="text" name="second" id="second">
第三个数:<input type="text" name="third" id="third"> 总计: <input type="button" name="sum" id="sum" readonly="readonly">
</form>
</body>
</html>现在要求用jqery来实现,三个文本框输入值相加为总计中得值,只要其中任何一个文本框中得值发生变化,总计中的值相应发生变化。
<html>
<body>
<form>
第一个数:<input type="text" name="first" id="first">
第二个数:<input type="text" name="second" id="second">
第三个数:<input type="text" name="third" id="third"> 总计: <input type="button" name="sum" id="sum" readonly="readonly">
</form>
</body>
</html>现在要求用jqery来实现,三个文本框输入值相加为总计中得值,只要其中任何一个文本框中得值发生变化,总计中的值相应发生变化。
解决方案 »
- document.getElementsByTagName添加JS调用后不成功
- 求居中分页显示函数
- 如何在javascript 中创建标签<a>,点击a标签内容,调用另外一个javascript函数?
- javascript中实现字符转换成日期,加一天后再转换成字符
- jQuery使用ajaxStop的问题
- 为img绑定onload事件问题
- EcmaScript正則表達式( 深入淺出系列之淺出 ^_^ )
- Google搜索结果自定义显示解决方案
- 请教一个radio事件
- javascript 可以读取服务器时间吗?(100分求)
- JS的函数对DOM的传值操作
- 怎么用js判断session存储的验证码
<body>
<form>
第一个数:<input type="text" name="first" id="first" onchange="fn()">
第二个数:<input type="text" name="second" id="second" onchange="fn()">
第三个数:<input type="text" name="third" id="third" onchange="fn()"> 总计: <input type="type" name="sum" id="sum" readonly="readonly">
</form>
</body>
<script>
var fEl = $("#first"),
sEl = $("#second"),
tEl = $("#third");
function fn(){
$("#sum").val(+fEl.val() + +sEl.val() + +tEl.val());
}</script>
这个写的偏离jquery 的本意了吧
<html> <body>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.min.js"></script>
<script>
$(document).ready(function(){
$('input').keyup(function(){
var first=parseInt($.trim($('#first').val()))||0;
var second=parseInt($.trim($('#second').val()))||0;
var third=parseInt($.trim($('#third').val()))||0;
$('#sum').val(first+second+third);
});
});
</script>
<form>
第一个数:<input type="text" name="first" id="first">
第二个数:<input type="text" name="second" id="second">
第三个数:<input type="text" name="third" id="third"> 总计: <input type="button" name="sum" id="sum" readonly="readonly">
</form>
</body>
</html>