1

私はこのスクリプトを持っています:

//CODE BELOW THIS POINT IS NOT MINE//

/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/

function CalculateTotal(frm) {
var order_total = 0

// 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

    //My attempt at updating order total for checkbox
           if(document.checkbox.select[i].checked)  
        {
            order_total = item_price

       }

        }
    }
}

// Display the total rounded to two decimal places
frm.TOTAL.value = round_decimals(order_total, 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
}
//CODE ABOVE THIS POINT IS NOT MINE//

そして、あなたはアイテムの価格を定義し、それはこれでIDです:

    <input type="text" name="PROD_CH_35.00" onChange="CalculateTotal(this.form)">

これはチェックボックス関数のコードです:

function totalcheckbox() {
document.checkbox.TOTAL.value = ''; //I set the value of all the checkboxes equal to nothing
var total = 0;
//For loop iterates through all the checkboxes. ++ adds one each time to i.
for (i=0;i<document.checkbox.select.length;i++) {
      if (document.checkbox.select[i].checked) {
        total = total + parseInt(document.checkbox.select[i].value); //take whatever they selected and make it into an integer
      }
    }
document.checkbox.TOTAL.value = total; //total value box equal to the sum
}

私のチェックボックスは次のようなものです:

    <input type="checkbox" name="select" value="15" onchange="totalcheckbox()">

私が抱えている問題は、2 つのフィールドがチェックボックスになっていることです。それらのチェックボックスを選択すると、注文合計に追加されるようにしたいです。チェックボックスの選択を解除すると、チェックボックスの価格が差し引かれます。

たとえば、次のようなチェックボックスを使用できます。

    <input type="checkbox" name="PROD_CH_15.00" value="15" onchange="totalcheckbox()">

それ自体で、チェックボックスの機能はそれ自体で合計を追加するように機能し、注文合計機能も同様に機能します。最大の問題は、合計を更新するテキストボックスに数量を入力した場合と同じようにチェックボックスが合計価格を変更するように、注文合計スクリプトを編集することです。

動作するチェックボックス コードの JS Fiddle は次のとおりです: http://jsfiddle.net/7uzr4/

Order Total Code の JS Fiddle は次のとおりです: http://jsfiddle.net/X2wMR/

チェックボックスのコードも注文合計に追加されるように、これら 2 つのコードを組み合わせるにはどうすればよいですか?

4

1 に答える 1

0

item_quantityフィールドタイプに基づいて設定する必要があります。あなたの currentCalculateFunctionはわずかな変更で動作するはずです:

// Get the quantity
if(form_field.type == 'checkbox') {
    // Will be 0 for unchecked, 1 for checked
    item_quantity = form_field.checked;
} else {
    item_quantity = parseInt(form_field.value, 10);           
}

次に、チェックボックスから同じ関数を呼び出すだけonchangeです:

<input type="checkbox" name="PROD_CH_15.00" value="15" onchange="CalculateTotal(this.form)">

jsfiddle の作業バージョン

于 2012-04-18T03:47:04.313 に答える