我想要獲取「(+ $」和「)」之間的金額並將其添加到輸入框的值。獲取金額並放到其他地方
例如,您選擇以下內容:
Solero異國情調(+ $ 1.85)
卡布奇諾(+ $ 2.49)
iMac的27英寸爲3.1GHz(+ $ 1,999.00)
這些金額將從中扣除選項您選擇:
1.85
2.49
1,999.00
的輸入框會DISPLA Y:2003.34
任何人都知道一個JavaScript代碼,可以做到這一點? (不添加多個屬性選項標籤)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Get amount and put elsewhere</title>
</head>
<body>
<form action="">
<fieldset>
<ul style="list-style:none;padding-left:0;">
<li>Ice Cream:
<select class="optionsIceCream" name="IceCream">
<option value="IceCream01">Select Ice Cream</option>
<option value="IceCream02">Solero Exotic (+$1.85)</option>
<option value="IceCream03">Magnum Ecuador (+$4.85)</option>
<option value="IceCream04">Cornetto Enigma (+$2.00)</option>
</select>
</li>
<li>Coffee:
<select class="optionsCoffee" name="Coffee">
<option value="Coffee01">Select Coffee</option>
<option value="Coffee02">Cup of Joe (+$0.99)</option>
<option value="Coffee03">Cappuccino (+$2.49)</option>
<option value="Coffee04">Latte Macchiato (+$2.99)</option>
</select>
</li>
<li>Computers:
<select class="optionsComputers" name="Computers">
<option value="Computer01">Select Computer</option>
<option value="Computer02">Dell Inspiron 620 (+$449.99)</option>
<option value="Computer03">HP Pavilion dv7t (+$949.99)</option>
<option value="Computer04">iMac 27-inch 3.1GHz (+$1,999.00)</option>
</select>
</li>
</ul>
Total: <input class="totalAmount" type="text" disabled="disabled" value="0.00" />
</fieldset>
</form>
</body>
</html>
* ** * ** * ** * * * * ** * ** 更新:09-09-2011 ** * ** * ** * ** * ** * ** *
我找到了一個解決方案,這要歸功於Dominic H從雅虎答案
這很簡單,但非常有效,它完美的作品!
這裏是整個代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Get amount and put elsewhere</title>
</head>
<body>
<form action="">
<fieldset>
<ul style="list-style:none;padding-left:0;">
<li>Ice Cream:
<select class="optionsIceCream" name="IceCream">
<option value="IceCream01">Select Ice Cream</option>
<option value="IceCream02">Solero Exotic (+$1.85)</option>
<option value="IceCream03">Magnum Ecuador (+$4.85)</option>
<option value="IceCream04">Cornetto Enigma (+$2.00)</option>
</select>
</li>
<li>Coffee:
<select class="optionsCoffee" name="Coffee">
<option value="Coffee01">Select Coffee</option>
<option value="Coffee02">Cup of Joe (+$0.99)</option>
<option value="Coffee03">Cappuccino (+$2.49)</option>
<option value="Coffee04">Latte Macchiato (+$2.99)</option>
</select>
</li>
<li>Computers:
<select class="optionsComputers" name="Computers">
<option value="Computer01">Select Computer</option>
<option value="Computer02">Dell Inspiron 620 (+$449.99)</option>
<option value="Computer03">HP Pavilion dv7t (+$949.99)</option>
<option value="Computer04">iMac 27-inch 3.1GHz (+$1,999.00)</option>
</select>
</li>
</ul>
Total: <input class="totalAmount" type="text" disabled="disabled" value="0.00" />
</fieldset>
</form>
<script type="text/javascript">
//<![CDATA[
(function() {
var selects = document.getElementsByTagName("select"),
L = selects.length,
i;
for (i = 0; i < L; i++) {
selects[i].setAttribute("onchange", "calcTotal();");
}
}());
function calcTotal() {
var icecream = [0.00, 1.85, 4.85, 2.00],
coffee = [0.00, 0.99, 2.49, 2.99],
computer = [0.00, 449.99, 949.99, 1999.00],
total = document.getElementsByTagName("input")[0],
select = document.getElementsByTagName("select");
total.value = (icecream[select[0].selectedIndex] +
coffee[select[1].selectedIndex] +
computer[select[2].selectedIndex]).toFixed(2);
}
//]]>
</script>
</body>
</html>
parseInt()將返回一個int。也就是說,parseInt('1.85')將返回1,而不是1.85。使用一元加運算符將字符串轉換爲數字:+'1.85'將給出1.85。 – Sjoerd
對不起,您可以使用parseFloat()或Number()將其轉換爲數字。 – Ibolit