在不工作的jquery表中添加动态数据

时间:2015-02-25 10:10:40

标签: jquery html

我以json格式从数据库中恢复数据。如你所见,我将它们添加到表中。在每次传递时,我将新创建的表格行传递给计算总和,该计算总和在所有细节都在那里被提醒时起作用。问题是总数没有更新,即使总和它可以回声。

function calculateRow(list) {
    var cost = 0;
    var $row = $(list).closest("tr");
    var weeks = parseFloat($row.find('.weeks').val());
    var quantity = parseFloat($row.find('.quantity').val());
    var price = parseFloat($row.find('.price').val());
    var mark = parseFloat($row.find('.mark').val());
    var ptype = $row.find('.ptype').val();
//alerts me of numbers here
alert('-'+weeks +'-'+quantity+'-'+price+'-'+mark+'-'+ptype);
        var mcond = weeks * quantity * price;
        $row.find('.total').val(mcond.toFixed(2));
        $row.find('.total-prod').val(mcond.toFixed(2));
//alerts me of total 
        alert(mcond);
}

现在我的新行的脚本

function editquote(){
        var list = "";
        $.getJSON('responder.php?method=edit-quote',function(data){
        $.each(data.products,function(index,item){
            list += '<tr class="copyIng"><td class="title"><select size="1" class="equip" name="item[]"  data-mini="true"><option value="'+item.pid+'">'+item.pname+'</option></select></td>'+
            '<td class="smallrow"><center><input type="text"  style="text-align: center;" class="weeks" name="weeks[]" value="'+item.pw+'" /></center><input type="hidden"  class="mark" name="mark[]" value="'+item.pmu+'" /><input type="hidden" class="ptype" name="ptype[]" value="'+item.pt+'" /></td>'+
            '<td class="smallrow"><center><input type="text" style="text-align: center;" class ="quantity" name="quantity[]" value="'+item.pq+'" /></center></td>'+
            '<td class="smallrow"><center><input type="text" style="text-align: center;" class ="price" name="price[]"  class="price" value="'+item.pc+'" /></center></td>'+
            '<td class="smallrow" style="background: rgba(0,0,0,0.1);"><center>'+
            '<input type="text" style="text-align: center;" class="total" name="total" value="0.00"  readonly="true" />'+
            '<input type="hidden" name="dm1" class="total-prod" value="0.00" />'+
            '<input type="hidden" name="dm2" class="total-trans" value="0.00" />'+
            '<input type="hidden" name="dm3" class="total-lab" value="0.00" />'+
            '</center></td>'+
            '<td class="smallrow"><center><a class="remove"><img src="red.png" alt="Smiley face" height="20" width="20"></a></center></td></tr>';
            $("#equipTable tr.grandtotal").before(list);
            $(".copyIng").trigger("create");
            calculateRow(list);
            $( "#equipTable" ).table( "refresh" );
            list ='';
        });
        });
    }

1 个答案:

答案 0 :(得分:1)

尝试更改此内容:

var $row = $(list).closest("tr");

对此:

var $row = $(list).siblings("tr").last();

.closest()遍历DOM时,但在您的情况下,您需要广度而不是深度。我添加了.last()方法,因为我假设您要更新最后一行,即最后一行<tr>。如果不是这样,请告诉我,我可以相应地调整我的答案。

修改

更改为editquote()这个(我刚添加了1行):

function editquote(){
        var list = "";
        $.getJSON('responder.php?method=edit-quote',function(data){
        $.each(data.products,function(index,item){
            list += '<tr class="copyIng"><td class="title"><select size="1" class="equip" name="item[]"  data-mini="true"><option value="'+item.pid+'">'+item.pname+'</option></select></td>'+
            '<td class="smallrow"><center><input type="text"  style="text-align: center;" class="weeks" name="weeks[]" value="'+item.pw+'" /></center><input type="hidden"  class="mark" name="mark[]" value="'+item.pmu+'" /><input type="hidden" class="ptype" name="ptype[]" value="'+item.pt+'" /></td>'+
            '<td class="smallrow"><center><input type="text" style="text-align: center;" class ="quantity" name="quantity[]" value="'+item.pq+'" /></center></td>'+
            '<td class="smallrow"><center><input type="text" style="text-align: center;" class ="price" name="price[]"  class="price" value="'+item.pc+'" /></center></td>'+
            '<td class="smallrow" style="background: rgba(0,0,0,0.1);"><center>'+
            '<input type="text" style="text-align: center;" class="total" name="total" value="0.00"  readonly="true" />'+
            '<input type="hidden" name="dm1" class="total-prod" value="0.00" />'+
            '<input type="hidden" name="dm2" class="total-trans" value="0.00" />'+
            '<input type="hidden" name="dm3" class="total-lab" value="0.00" />'+
            '</center></td>'+
            '<td class="smallrow"><center><a class="remove"><img src="red.png" alt="Smiley face" height="20" width="20"></a></center></td></tr>';
            //NEW LINE
            list = $(list);
            $("#equipTable tr.grandtotal").before(list);
            $(".copyIng").trigger("create");
            calculateRow(list);
            $( "#equipTable" ).table( "refresh" );
            list ='';
        });
        });
    }

然后将var $row更改为:

var $row = list;

问题是list是一个字符串,所以当你将它传递给calculateRow()时,$row成为一个新元素,而不是对插入到DOM中的元素的引用。