关于遍历的值,求和的有关问题
关于遍历<input>的值,求和的问题
test.jsp
运行截图:
需要实现的功能:
只要输入重量,自动求出金额,写入对应的<input>,并且在最后一行合计中统计每列的合计,如过没有填写重量,则所在行的金额为空
------解决思路----------------------
------解决思路----------------------
------解决思路----------------------
test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'test.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table>
<tr>
<td>单价</td>
<td>重量</td>
<td>金额</td>
</tr>
<tr>
<td>3</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
<tr>
<td>4</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
<tr>
<td>5</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
<tr>
<td>合计</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
</table>
</body>
</html>
运行截图:
需要实现的功能:
只要输入重量,自动求出金额,写入对应的<input>,并且在最后一行合计中统计每列的合计,如过没有填写重量,则所在行的金额为空
------解决思路----------------------
<html>
<head>
<title>Index</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://cloud.xing-xing.com/jquery.js"></script>
<style>
</style>
</head>
<body>
<table>
<tr>
<td>单价</td>
<td>重量</td>
<td>金额</td>
</tr>
<tr>
<td>3</td>
<td><input type="text" name="weight"></td>
<td><input type="text"></td>
</tr>
<tr>
<td>4</td>
<td><input type="text" name="weight"></td>
<td><input type="text"></td>
</tr>
<tr>
<td>5</td>
<td><input type="text" name="weight"></td>
<td><input type="text" name=""></td>
</tr>
<tr>
<td>合计</td>
<td><input type="text" name="weight"></td>
<td><input type="text"></td>
</tr>
</table>
</body>
<script>
$(function(){
$('input[name=weight]').blur(function(){
var price = $(this).parent().prev().html();
var sumInput = $(this).parent().next().find('input');
var weight = $(this).val();
$(sumInput).val(price * weight);
});
});
</script>
</html>
------解决思路----------------------
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
<table>
<tr>
<td>单价</td>
<td>重量</td>
<td>金额</td>
</tr>
<tr>
<td>3</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
<tr>
<td>4</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
<tr>
<td>5</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
<tr>
<td>合计</td>
<td><input id="c1" type="text"></td>
<td><input id="c2" type="text"></td>
</tr>
</table>
<script>
var d=$('table tr:not(:last) td:nth-child(2n) input').change(function(){
d.each(function(){ $(this).parent().next().find('input').val((parseFloat(this.value)*parseFloat($(this).parent().prev().text()))
------解决思路----------------------
'');
});
$('#c1').val(eval(d.map(function(){return parseFloat(this.value)
------解决思路----------------------
0}).get().join('+')));
$('#c2').val(eval($('table tr:not(:last) td:nth-child(2n+1) input').map(function(){return parseFloat(this.value)
------解决思路----------------------
0}).get().join('+')));
})
</script>
------解决思路----------------------
$(function(){
var w_inps = $('tr:gt(0):not(:last) td:nth-child(2)>:text');
var m_inps = $('tr:gt(0):not(:last) td:nth-child(3)>:text');
w_inps.keyup(function(){
var w = this.value,d = $(this).parent().prev().text() * 1;
if(w){
$(':text',$(this).parent().next()).val(w * d);
$('tr:last :text:eq(0)').val(eval(w_inps.map(function(){return this.value
------解决思路----------------------
0 }).get().join('+')));
$('tr:last :text:eq(1)').val(eval(m_inps.map(function(){return this.value
------解决思路----------------------
0 }).get().join('+')));
}
})
})