だから私は、人々がさまざまな数量のアイテムを選択できる注文フォームに取り組んでおり、JSを使用して合計を計算しています。たとえば、2 つの茶色の帽子 (それぞれ $15) を選択すると、TOTAL は自動的に $30 になります。POST ページで、テキスト ボックスの名前を取得して、2 つの茶色の帽子を注文したことを知ることができます。
ここに私の問題があります: ラジオボタンオプションを設定して、1 つの項目のみを選択できるようにし、最終的な合計に追加したいのは、次のようなものです。
<input type="radio" value="0" name="Syllabus" checked="No" /> Paper ($0)<br />
<input type="radio" value="0" name="Syllabus" checked="No" /> USB ($0)<br />
<input type="radio" value="20" name="Syllabus" checked="No" /> Both ($20)<br />
「両方」が選択されている場合、JSを使用して合計に20ドルを追加するにはどうすればよいですか? もちろん、他のオプションのいずれかを選択した場合は、20 ドルが差し引かれます。最後に、彼らが PAPER または USB を選択した場合、どちらの方法でも値が 0 になるため、最終出力で彼らが選択したものをどのように知ることができますか?
助けてくれてありがとう!
------ 誰でも役立つ場合は、他のテキスト ボックスの値を追加するために使用するコードをここに示します。次に、合計をゼロで埋めて丸めます ------
// JavaScript Document
function CalculateTotal(frm) {
var order_total = 0
var syllabuscost;
var radios = document.getElementsByTagName('input');
var radiovalue;
for (var z = 0; z < radios.length; z++) {
if (radios[z].type === 'radio' && radios[z].checked) {
// get value, set checked flag or do whatever you need to
radiovalue = radios[z].value;
//Change the syllabus cost depending on the value of the radio button
if (radiovalue == "Paper" || radiovalue == "USB") {
syllabuscost = 0
}
else if (radiovalue == "Both") {
syllabuscost = 20
}
}
}
// Run through all the form fields
for (var i=0; i < frm.elements.length; ++i) {
// Get the current field
form_field = frm.elements[i]
// Get the field's name
form_name = form_field.name
// Is it a "product" field?
if (form_name.substring(0,4) == "PROD") {
// If so, extract the price from the name
item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))
// Get the quantity
item_quantity = parseInt(form_field.value)
// Update the order total
if (item_quantity >= 0) {
order_total += (item_quantity * item_price)
}
}
}
// Display the total rounded to two decimal places Also added the syllabuscost to the total price!
frm.TOTAL.value = round_decimals(order_total+syllabuscost, 2)
}
}
function round_decimals(original_number, decimals) {
var result1 = original_number * Math.pow(10, decimals)
var result2 = Math.round(result1)
var result3 = result2 / Math.pow(10, decimals)
return pad_with_zeros(result3, decimals)
}
function pad_with_zeros(rounded_value, decimal_places) {
// Convert the number to a string
var value_string = rounded_value.toString()
// Locate the decimal point
var decimal_location = value_string.indexOf(".")
// Is there a decimal point?
if (decimal_location == -1) {
// If no, then all decimal places will be padded with 0s
decimal_part_length = 0
// If decimal_places is greater than zero, tack on a decimal point
value_string += decimal_places > 0 ? "." : ""
}
else {
// If yes, then only the extra decimal places will be padded with 0s
decimal_part_length = value_string.length - decimal_location - 1
}
// Calculate the number of decimal places that need to be padded with 0s
var pad_total = decimal_places - decimal_part_length
if (pad_total > 0) {
// Pad the string with 0s
for (var counter = 1; counter <= pad_total; counter++)
value_string += "0"
}
return value_string
}
そしてHTML
<form>
<tr>
<th><label for="PROD_BrownShoe_175">Brown Shoe</label></th>
<td><input type="text" name="PROD_BrownShoe_175" onchange="CalculateTotal(this.form)" /> ($175) - Each</td>
</tr>
<tr>
<th><label for="PROD_BlackShoe_255">Black Shoe</label></th>
<td><input type="text" name="PROD_BlackShoe_255" onchange="CalculateTotal(this.form)" /> ($255) - Each</td>
</tr>
<tr>
<th><label for="PROD_BlueShoe_325">Blue Shoe</label></th>
<td><input type="text" name="PROD_BlueShoe_325" onchange="CalculateTotal(this.form)" /> ($325) - Each</td>
</tr>
<tr>
<th>Total $:</th>
<td><input type="text" name="TOTAL" onFocus="this.form.elements[0].focus()" /></td>
</tr>
</form>