0
我使用這個代碼如何乘用javascript,並將結果輸出到另一個文本字段兩個文本字段
<script type="text/javascript">
$(function() {
$("#addAll2").click(function() {
var add = 0;
$("#discount") = $dis
$(".amt2").each(function() {
add += document.getElementById('subtotal').value * document.getElementById('discount').value/100;
});
$("#discountamt").val(add.toFixed(2));
});
});
</script>
<script type="text/javascript">
$(function() {
$("#addAll").click(function() {
var add = 0;
$(".amt").each(function() {
add += Number($(this).val());
});
$("#subtotal").val(add.toFixed(2));
});
});
</script>
<script type="text/javascript">
$(function() {
$("#addAll3").click(function() {
var add = 0;
$(".amt3").each(function() {
add += Number($(this).val());
});
$("#sum").val(add.toFixed(2));
});
});
</script>
隨着這些領域:
<fieldset>
<legend>Items</legend>
<table>
<thead>
<tr>
<th scope="col" width="100px">ALU</th>
<th scope="col" width="400px">Item Name</th>
<th scope="col" width="100px">Item Price</th>
</tr>
</thead>
<tbody>
<tr>
<td><input name="alu1" id="alu1" type="text" size="15" /></td>
<td><input name="itemname1" id="itemname1" type="text" size="75" /></td>
<td>$
<input name="price1" id="price1" class="currency amt amt1" type="text" size="14" value="0.00" /></td>
</tr>
</tbody>
</table>
<button>Add Row</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function($)
{
// trigger event when button is clicked
$("button").click(function()
{
// add new row to table using addTableRow function
addTableRow($("table"));
// prevent button redirecting to new page
return false;
});
// function to add a new row to a table by cloning the last row and
// incrementing the name and id values by 1 to make them unique
function addTableRow(table)
{
// clone the last row in the table
var $tr = $(table).find("tbody tr:last").clone();
// get the name attribute for the input and select fields
$tr.find("input,select").attr("name", function()
{
// break the field name and it's number into two parts
var parts = this.id.match(/(\D+)(\d+)$/);
// create a unique name for the new field by incrementing
// the number for the previous field by 1
return parts[1] + ++parts[2];
// repeat for id attributes
}).attr("id", function(){
var parts = this.id.match(/(\D+)(\d+)$/);
return parts[1] + ++parts[2];
});
// append the new row to the table
$(table).find("tbody tr:last").after($tr);
};
});
</script>
<div class="fieldline" style="text-align:right;">
<input id="addAll" type="button" value="Calculate Subtotal" />
<label>Subtotal: $
<input size="18" name="subtotal" id="subtotal" class="amt2 amt3" type="text" value="0.00" />
</label>
</div>
<div class="fieldline" style="text-align:right;">
<label>Discount:
<input size="18" name="discount" id="discount" type="text" value="00" />%
</label>
</div>
<div class="fieldline" style="text-align:right;">
<input id="addAll2" type="button" value="Calculate Discount" />
<label>Discount Amount: $
<input size="18" name="discountamt" id="discountamt" class="amt3" type="text" value="0.00" />
</label>
</div>
<div class="fieldline" style="text-align:right;">
<label>Shipping Method:
<select name="shipmeth" id="shipmeth" onchange="MM_jumpMenu('parent',this,0)">
<option></option>
<option value="freeups">FREE UPS Ground</option>
<option value="upsground">UPS Ground</option>
<option value="ups2">UPS 2nd Day</option>
<option value="ups3">UPS 3rd Day</option>
<option value="ups1">UPS Next Day Air</option>
<option value="upscanada">UPS Canada</option>
<option value="uspsint">USPS International</option>
<option value="dhlint">DHL International</option>
<option value="freeship">Free Shipping Override</option>
</select>
</label>
</div>
<div class="fieldline" style="text-align:right;">
<label>Shipping Cost: $
<input size="18" name="shipcost" id="shipcost" class="amt3" type="text" value="0.00" />
</label>
</div>
<div class="fieldline" style="text-align:right;">
<input id="addAll1" type="button" value="Calculate CA TAX" />
<label>Sales Tax: $
<input name="salestax" type="text" id="salestax" class="amt3" size="18" value="0.00" />
</label>
</div>
<div class="fieldline" style="text-align:right;">
<input id="addAll" type="button" value="Calculate Total" />
<label>Total:
<input size="18" required="required" name="sum" id="sum" type="text" value="0.00" />
</label>
</div>
</fieldset>
我有它,所以它calulates的小計,並將其放入。但是,當我嘗試允許它將折扣字段乘以折扣並將其乘以100後,將折扣Ammount字段中的結果除以100。它似乎沒有工作。我也嘗試過不同的變化。
如果我能得到折扣,我應該可以計算剩餘的總額字段。
謝謝!
您似乎在頁面上有重複的ID。還要添加或乘數使用parseFloat(num) –
@ Sushanth-parseFloat旨在從十進制數的末尾修剪非數字字符,在用戶輸入正在處理的位置不是必需的(並且可能有害)。這在這裏似乎不太合適。 [乘法運算符(*)](http://ecma-international.org/ecma-262/5.1/#sec-11.5.1)將強制操作數爲數字。 – RobG
@Dak Washbrook - 請定義「不起作用」。並且請發佈一個顯示問題的最小測試案例,而不是大量的代碼和標記塊,通常這會在您發佈之前引導您找到解決方案。 – RobG