2013-11-27 169 views
0

我有一个网页,用户可以根据从下拉列表中选择的项目搜索并选择特定类型的纸张。然后搜索将返回并显示纸张的全名以及所有从我的数据库中提取的价格和每包值。我需要计算(最好是动态的)总价格是(quantity * price)/per_pack。因此,当用户更改数量时,总价格会发生变化。我所遇到的几种解决方案,自己的方案中,其工作然而,我想知道是否有办法做到这一点,而无需指定的值,例如属性价格:计算总价格

不喜欢这样:

<input type="text" name="price" id="price" class="txt" size="10" maxlength="9" value="250" /> 

我试图做这样的事情,但它只是似乎没有工作,因为在当我输入数量好像没有什么改变,总箱只是空白:

的JavaScript:

$(function() { 
window.globalVar = 0; 

// Skip the filled description boxes 
for (var i=0; i<10; i++) 
{ 
    if($('#description_'+window.globalVar).val() != "") 
    { 
     window.globalVar++; 
    } 
} 

// Write the paper description and price for the selected paper 
function log(message) { 
    var values = message.split('|'); 
    $('#description_'+window.globalVar).val(values[0]); 
    $('#priceper_'+window.globalVar).val(values[1]); 
$('#per_pack_'+window.globalVar).val(values[2]); 
    window.globalVar++; 
    console.log(window.globalVar); 
} 

// Search the Paper db 
$("#supplier_search").autocomplete({ 
    source: function(request, response) { 
    $.ajax({ 
     url: "http://mpc.vario/mis_pp/common/pp_json", 
     dataType: "jsonp", 
     data: { 
     featureClass: "P", 
     style: "full", 
     maxRows: 25, 
     name_startsWith: request.term, 
     supplier: $('#supplier').val() 
     }, 
     success: function(data) { 
     console.log(data); 
     response($.map(data, function(item) { 
      return { 
      label: item.name, 
      value: item.name + '|' + item.value 
      } 
     })); 
     } 
    }); 
    }, 
    minLength: 2, 
    select: function(event, ui) { 
    log(ui.item.value); 
    $(this).val(''); 
    return false; 
    }, 
    open: function() { 
    $(this).removeClass("ui-corner-all").addClass("ui-corner-top"); 
    }, 
    close: function() { 
    $(this).removeClass("ui-corner-top").addClass("ui-corner-all"); 
    } 
}); 
}); 

// Calculate total 
var sum = 0; 
document.getElementById('priceper_0').value = sum; 
function OnChange(value) 
{ 
price = document.getElementById('priceper_0').value; 
    per_pack = document.getElementById('per_pack_0').value; 
quantity = document.getElementById('quantity_0').value; 
sum = (price * quantity)/per_pack; 


} 

HTML:在错误的地方

<tr class="multipp"> 
<td><input type="text" name="description_0" id="description_0" size="85" maxlength="70" value="<?php echo htmlspecialchars($description[0]); ?>" /></td> 
<td><input type="text" name="priceper_0" id="priceper_0" size="10" maxlength="9" value="" /></td> 
<td><input type="text" name="per_pack_0" id="per_pack_0" class="txt" size="10" maxlength="9" value="" /></td> 
<td><input type="text" name="quantity_0" id="quantity_0" class="quantity_0" size="10" maxlength="9" value="" onchange="return OnChange();" /></td> 
<td><input type="text" name="subtotal_0" id="subtotal_0" class="txt" size="10" maxlength="9" value="" /></td> 
</tr> 
+0

的document.getElementById( '价格')值=总和。你有没有试过使用onkeyup? – CaffeineShots

+0

为什么使用MySQL标签? – Strawberry

+0

@Strawberry,因为价格是从数据库中使用mysql语句在php – user3009232

回答

1

您调用JS功能,更改:

<input type="text" name="quantity_0" id="quantity_0" class="quantity_0" size="10" maxlength="9" value="OnChange()"/> 

<input type="text" name="quantity_0" id="quantity_0" class="quantity_0" size="10" maxlength="9" value="" onchange="return OnChange();"/> 
+0

我加onchange =“返回OnChange();但是,我仍然没有得到任何东西 – user3009232

1

的问题是在调用方法

替换以下

value="OnChange()" 

onchange="OnChange()"