使用 jquery.calculation.js 和动态添加的表单字段
我正在创建一个带有动态添加字段的表单,并希望对所有添加的字段进行求和。到目前为止,我有以下代码,但它只总结了第一行:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Totals</title>
<script type="text/javascript" src="../js/jquery/jquery-1.6.2.js"></script>
<script type="text/javascript" src="../js/jquery/jquery.calculation.js"></script>
<script type="text/javascript">
//Sum
$(document).ready(
function (){
$('input[name^=reds]').sum("keyup", "#totalSumReds");
$("input[name^=blues]").sum("keyup", "#totalSumBlues");
}
);
</script>
<script type="text/javascript">
//Add/remove form fields
jQuery(function($){
$('#btnAdd').click( function(){
var num = $('.clonedInput').length;
var newNum = new Number(num + 1);
var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
newElem.children(':first').attr('id', 'name' + newNum).attr('reds', 'reds' + newNum);
$('#input' + num).after(newElem);
$('#btnDel').attr('disabled', false);
if (newNum == 5)
$('#btnAdd').attr('disabled', 'disabled');
});
$('#btnDel').click( function() {
var num = $('.clonedInput').length;
$('#input' + num).remove();
$('#btnAdd').attr('disabled', false);
if (num-1 == 1 )
$('#btnDel').attr('disabled', 'disabled');
});
$('#btnDel').attr('disabled', 'disabled');
});
</script>
</head>
<body>
<form id="myForm">
<div id="input1" style="margin-bottom:4px;" class="clonedInput">
Numbers: <input type="text" name="reds"/>
<input type="text" name="blues" id="blues"/>
</div>
<div>
Totals: <input type="text" name="totalSumReds" id="totalSumReds" value="" size="2" readonly="readonly" />
<input type="text" name="totalSumBlues" id="totalSumBlues" value="" size="2" readonly="readonly" />
</div>
<div>
<input type="button" id="btnAdd" value="add line" />
<input type="button" id="btnDel" value="remove line" />
</div>
</form>
</body>
</html>
关于我应该尝试修复它的任何想法吗?谢谢!
I'm creating a form with dynamically added fields and would like to sum all added fields. So far I have the following code, but it only sums the first row:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Totals</title>
<script type="text/javascript" src="../js/jquery/jquery-1.6.2.js"></script>
<script type="text/javascript" src="../js/jquery/jquery.calculation.js"></script>
<script type="text/javascript">
//Sum
$(document).ready(
function (){
$('input[name^=reds]').sum("keyup", "#totalSumReds");
$("input[name^=blues]").sum("keyup", "#totalSumBlues");
}
);
</script>
<script type="text/javascript">
//Add/remove form fields
jQuery(function($){
$('#btnAdd').click( function(){
var num = $('.clonedInput').length;
var newNum = new Number(num + 1);
var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
newElem.children(':first').attr('id', 'name' + newNum).attr('reds', 'reds' + newNum);
$('#input' + num).after(newElem);
$('#btnDel').attr('disabled', false);
if (newNum == 5)
$('#btnAdd').attr('disabled', 'disabled');
});
$('#btnDel').click( function() {
var num = $('.clonedInput').length;
$('#input' + num).remove();
$('#btnAdd').attr('disabled', false);
if (num-1 == 1 )
$('#btnDel').attr('disabled', 'disabled');
});
$('#btnDel').attr('disabled', 'disabled');
});
</script>
</head>
<body>
<form id="myForm">
<div id="input1" style="margin-bottom:4px;" class="clonedInput">
Numbers: <input type="text" name="reds"/>
<input type="text" name="blues" id="blues"/>
</div>
<div>
Totals: <input type="text" name="totalSumReds" id="totalSumReds" value="" size="2" readonly="readonly" />
<input type="text" name="totalSumBlues" id="totalSumBlues" value="" size="2" readonly="readonly" />
</div>
<div>
<input type="button" id="btnAdd" value="add line" />
<input type="button" id="btnDel" value="remove line" />
</div>
</form>
</body>
</html>
Any ideas about what should I try to fix it? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的代码中,您仅在现有输入字段上注册求和函数,之后添加的任何字段都不会触发计算,并且在求和时也不会被考虑在内。
您可以使用事件委托来代替:
In your code you are registering the summing function only on the existing input fields, any fields added afterwards won't trigger the calculation and also won't be taken into account when summing.
You could use event delegation instead: