簡體   English   中英

總金額顯示為NAN,javascript

[英]total amount is getting displayed as NAN,javascript

我有一個表單,其中有兩組單選按鈕和一個文本框來輸入數量值。 基於選中的單選按鈕和“數量”文本框中的值,應在總文本框中填充值。 我正在使用數組進行計算,但總計字段中的值顯示為NAN。 誰能檢查我的代碼並讓我知道我哪里錯了?

**HTML**

<form id="orderform" name="orderform" >
                    <fieldset data-role="controlgroup">
                         <legend><b>Choice</b></legend>
                         <input type="radio" name="choice" id="veg" value="1" checked onclick="total()">
                         <label for="veg">Vegetarian</label>
                         <input type="radio" name="choice" id="nonveg" value="2" onclick="total()">
                         <label for="nonveg">Meat Lover</label>
                     </fieldset>
                    <fieldset data-role="controlgroup">
                        <legend><b>Size</b></legend>
                        <input type="radio" name="size" id="small" onclick="total()">
                        <label for="small">Small</label>
                        <input type="radio" name="size" id="medium" onclick="total()">
                        <label for="medium">Medium</label>
                        <input type="radio" name="size" id="large" onclick="total()">
                        <label for="large">Large</label><span class="validation"></span>
                    </fieldset>
                            <div><b>Quantity</b>
                        <input type="text" id ="qty" name="qty" size="5" onclick="total()">
                    </div><div>
                        <b>Total</b>
                        <input type="text" id="amt" name="amt" readonly="readonly"> 
                    </div>

Javascript Javascript

var array=[[8.99,9.99,10.99],[9.99,10.99,11.99]];

    function total()
    {
        var row = document.querySelector('input[name="choice"]:checked').value;
       var column = document.querySelector('input[name="size"]:checked').value;
        var qty=document.getElementById("qty").value;
        var total=0;  
            total= (array[row][column]+1)*qty;
       document.orderform.amt.value = total;
    }

您必須像下面那樣在html中更改大小組

         <legend><b>Size</b></legend>
         <input type="radio" name="size" id="small" value="1" onclick="total()">
         <label for="small">Small</label>
         <input type="radio" name="size" id="medium" value="2" onclick="total()">
         <label for="medium">Medium</label>
         <input type="radio" name="size" id="large" value="3" onclick="total()">
         <label for="large">Large</label><span class="validation"></span>

它會為你工作

var column = document.querySelector('input[name="size"]:checked').value;

您可以警報(列),您會發現該值是“ on”。 因為您沒有在所有尺寸組中設置任何值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM