问:大家好,
我有以下代码...
我想要它做但不能做的是每次将数字更改或添加新行时将所有价格字段相加并更改总和...但是它不起作用?
谁能看到我的问题?可以帮我解决这个问题吗?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="./jquery-1.7.min.js"></script>
<script type="text/javascript">
function calculateSum() {
var sum = 0;
$("#price").each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
$("#sum").html('£' + sum.toFixed(2));
}
$(document).ready(function(){
$('.del').live('click',function(){
$(this).parent().parent().remove();
});
$('.add').live('click',function(){
$(this).val('Delete');
$(this).attr('class','del');
var appendTxt = "<tr><td><input name='item[]' type='text' size='40' /></td><td><input name='qty[]' type='text' size='5' value='0' /></td><td><input name='price[]' id='price' type='text' size='20' value='0.00' /></td><td><input type='button' class='add' value='Add Row' /></td></tr>";
$("tr:last").after(appendTxt);
});
$('#price').keyup(function() {
calculateSum();
});
});
</script>
</head>
<body>
<table id="options-table">
<tr>
<td>Item</td>
<td>Qty</td>
<td>Price</td>
<td> </td>
</tr>
<tr>
<td><input name="item[]" type="text" size="40" /></td>
<td><input name="qty[]" type="text" value="0" size="5" /></td>
<td><input name="price[]" id="price" type="text" value="0.00" size="20" /></td>
<td><input type="button" class="add" value="Add Row" /></td>
</tr>
</table>
<br /><br />
<div id="sum">£0.00</div>
</body>
</html>
感谢
答:一个小疏忽:
$('#price').keyup(function() {
calculateSum();
});
仅将事件绑定一次,您也应该在live这里使用它:
$('price').live('keyup', function() {
calculateSum();
});
在我的示例中,我添加class="price"并使用.price而不是#price。使用ID的目的是指定单个项目。在您的情况下,您不止一个,所以使用class。我的整个代码如下所示:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
function calculateSum() {
var sum = 0;
$(".price").each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
$("#sum").html('£' + sum.toFixed(2));
}
$(document).ready(function(){
$('.del').live('click',function(){
$(this).parent().parent().remove();
});
$('.add').live('click',function(){
$(this).val('Delete');
$(this).attr('class','del');
var appendTxt = "<tr><td><input name='item[]' type='text' size='40' /></td><td><input name='qty[]' type='text' size='5' value='0' /></td><td><input class='price' name='price[]' id='price' type='text' size='20' value='0.00' /></td><td><input type='button' class='add' value='Add Row' /></td></tr>";
$("tr:last").after(appendTxt);
});
$('.price').live('keyup', function() {
calculateSum();
});
});
</script>
</head>
<body>
<table id="options-table">
<tr>
<td>Item</td>
<td>Qty</td>
<td>Price</td>
<td> </td>
</tr>
<tr>
<td><input name="item[]" type="text" size="40" /></td>
<td><input name="qty[]" type="text" value="0" size="5" /></td>
<td><input name="price[]" class="price" id="price" type="text" value="0.00" size="20" /></td>
<td><input type="button" class="add" value="Add Row" /></td>
</tr>
</table>
<br /><br />
<div id="sum">£0.00</div>
</body>
</html>