1

だから私は、人々がさまざまな数量のアイテムを選択できる注文フォームに取り組んでおり、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>
4

3 に答える 3

1

代わりに、ドルの値をオブジェクトに格納できます。

var costs = {
    "paper" : 0,
    "usb"   : 0,
    "both"  : 0
};

ラジオ ボタンの値は、一意にすることができます (一意にする必要があります)。

<input type="radio" value="paper" name="Syllabus" checked="No" /> Paper ($0)<br />

<input type="radio" value="usb" name="Syllabus" checked="No" /> USB ($0)<br />

<input type="radio" value="both" name="Syllabus" checked="No" /> Both ($20)<br />

そして、あなたの CalculateTotal 関数で、参照costs["paper"]など.

于 2012-07-23T15:52:36.800 に答える
0

この正確な理由から、値は価格だけでなく製品名にする必要があります。

解決策として、区切り記号で区切られた値フィールドに名前と価格の両方を含めることができます。これにより、十分な情報が得られます。

<input type="radio" value="paper|0" name="Syllabus" checked="No" /> Paper ($0)<br />
<input type="radio" value="usb|0" name="Syllabus" checked="No" /> USB ($0)<br />
<input type="radio" value="both|20" name="Syllabus" checked="No" /> Both ($20)<br />

スクリプトで値にアクセスする:

item_quantity = parseInt(form_field.value.split('|')[1])
于 2012-07-23T15:50:49.170 に答える
0

実際、私は別のルートに行き、問題を解決しました。ページ上のすべてのラジオ ボタンを通過するように設定し、「紙」、「usb」、または「両方」の値が見つかったかどうかに応じてシラバス コストを変更するスニペットを追加しました。

これにより、同じタイプの機能を持つラジオ ボタンをさらに追加する必要がある場合に、この全体をスケールアップできます。

これを行うためのよりクリーンで効率的な方法があるかもしれませんが、これが私が思いつく最善の方法です。私が得た回答は少し役に立ちましたが、実際には問題の解決には役立ちませんでした. これが他の誰かに役立つことを願っています。

于 2012-07-24T15:32:01.437 に答える