0

这是初始化我的表的代码:

<table cellspacing="0">
        <caption id="title4">
            Sprinkles (5 - 6 oz.)
                    </caption>
        <thead>
            <tr>
                <th>&nbsp;</th>
                                    <td >Price</td>
                                    <td >Quantity</td>
                            </tr>
        </thead>
        <tbody>
                        <tr class="statement4">
                <th><label>Lemon</label></th>
                                <td title="Yes" >
                    <label>$6.50</label>
                </td>
                                <td title="No" >
                    <input id="s1" name="s1" class="field text small" type="text" value="0"/>
                </td>
                            <tr class="statement4">
                <th><label>Sweet Apple</label></th>
                                <td title="Yes" >
                    <label>$6.50</label>
                </td>
                                <td title="No" >
                    <input id="s2" name="s2" class="field text small" type="text" value="0"/>
                </td>

                <tr class="statement4">
                <th><label>Sweet Caramel</label></th>
                                <td title="Yes" >
                    <label>$6.50</label>
                </td>
                                <td title="No" >
                    <input id="s3" name="s3" class="field text small" type="text" value="0"/>
                </td>

                <tr class="statement4">
                <th><label>Sweet Cinnamon</label></th>
                                <td title="Yes" >
                    <label>$6.50</label>
                </td>
                                <td title="No" >
                    <input id="s4" name="s4" class="field text small" type="text" value="0"/>
                </td>

                <tr class="statement4">
                <th><label>Sweet Mocha</label></th>
                                <td title="Yes" >
                    <label>$6.50</label>
                </td>
                                <td title="No" >
                    <input id="s5" name="s5" class="field text small" type="text" value="0"/>
                </td>
                        </tr>
                    </tbody>
    </table>

这是我的javascript:

<script type="text/javascript">
var total = 0.0;
var all=new Array();
for(var i = 0; i < 49; i++)
    all[i] = 0;

//....I OMMITTED CODE HERE FOR THE REST OF THE onkeyup FUNCTIONS
document.getElementById("s1").onkeyup = function() {
    if(this.value != "") {
        all[44] = (parseInt(this.value,10) * 6.50);
    }
    else {
        all[44] = 0;
    }
    updateIt();
};

document.getElementById("s2").onkeyup = function() {
    if(this.value != "") {
        all[45] = (parseInt(this.value,10) * 6.50);
    }
    else {
        all[45] = 0;
    }
    updateIt();
};

document.getElementById("s3").onkeyup = function() {
    if(this.value != "") {
        all[46] = (parseInt(this.value,10) * 6.50);
    }
    else {
        all[46] = 0;
    }
    updateIt();
};
document.getElementById("s4").onkeyup = function() {
    if(this.value != "") {
        all[47] = (parseInt(this.value,10) * 6.50);
    }
    else {
        all[47] = 0;
    }
    updateIt();
};

document.getElementById("s5").onkeyup = function() {
    document.getElementById("mySpan").innerHTML = "WOAH";

    if(this.value != "") {
        all[48] = (parseInt(this.value,10) * 6.50);
    }
    else {
        all[48] = 0;
    }
    updateIt();
};

function updateIt() {
    var theTotal = 0;
    for(var j = 0; j < 49; j++)
        theTotal += all[j];

    theTotal = all[48];
    document.getElementById("mySpan").innerHTML = "$" + theTotal.toFixed(2);
}
</script>

请注意,我声明了更多表和其他 onKeyUp 函数,但由于某种原因,此表之后的所有表(我有大约 6 个表,前 3 个正常工作),不响应它们各自的 onkeyup 事件。

这是最后一个工作表的代码(此代码就在创建我上面发布的表的代码的正上方):

<table cellspacing="0">
    <caption id="title4">
        Rubs (1.2 - 2.3oz.)
                </caption>
    <thead>
        <tr>
            <th>&nbsp;</th>
                                <td >Price</td>
                                <td >Quantity</td>
                        </tr>
    </thead>
    <tbody>
                    <tr class="statement4">
            <th><label>Buffalo</label></th>
                            <td title="Yes" >
                <label>$4.25</label>
            </td>
                            <td title="No" >
                <input id="rubs1" name="rubs1" class="field text small" type="text" value="0"/>
            </td>
                        <tr class="statement4">
            <th><label>Chipotle</label></th>
                            <td title="Yes" >
                <label>$4.25</label>
            </td>
                            <td title="No" >
                <input id="rubs2" name="rubs2" class="field text small" type="text" value="0"/>
            </td>

            <tr class="statement4">
            <th><label>Citrus</label></th>
                            <td title="Yes" >
                <label>$4.25</label>
            </td>
                            <td title="No" >
                <input id="rubs3" name="rubs3" class="field text small" type="text" value="0"/>
            </td>

            <tr class="statement4">
            <th><label>Creole</label></th>
                            <td title="Yes" >
                <label>$4.25</label>
            </td>
                            <td title="No" >
                <input id="rubs4" name="rubs4" class="field text small" type="text" value="0"/>
            </td>

            <tr class="statement4">
            <th><label>Crushed Peppercorn & Garlic</label></th>
                            <td title="Yes" >
                <label>$4.25</label>
            </td>
                            <td title="No" >
                <input id="rubs5" name="rubs5" class="field text small" type="text" value="0"/>
            </td>

            <tr class="statement4">
            <th><label>Greek</label></th>
                            <td title="Yes" >
                <label>$4.25</label>
            </td>
                            <td title="No" >
                <input id="rubs6" name="rubs6" class="field text small" type="text" value="0"/>
            </td>

            <tr class="statement4">
            <th><label>Jamaican Jerk</label></th>
                            <td title="Yes" >
                <label>$4.25</label>
            </td>
                            <td title="No" >
                <input id="rubs7" name="rubs7" class="field text small" type="text" value="0"/>
            </td>

            <tr class="statement4">
            <th><label>Lemon Pepper</label></th>
                            <td title="Yes" >
                <label>$4.25</label>
            </td>
                            <td title="No" >
                <input id="rubs8" name="rubs8" class="field text small" type="text" value="0"/>
            </td>

            <tr class="statement4">
            <th><label>Moroccan/label></th>
                            <td title="Yes" >
                <label>$4.25</label>
            </td>
                            <td title="No" >
                <input id="rubs9" name="rubs9" class="field text small" type="text" value="0"/>
            </td>

            <tr class="statement4">
            <th><label>Smoky Barbecue</label></th>
                            <td title="Yes" >
                <label>$4.25</label>
            </td>
                            <td title="No" >
                <input id="rubs10" name="rubs10" class="field text small" type="text" value="0"/>
            </td>

            <tr class="statement4">
            <th><label>Thai Red Curry</label></th>
                            <td title="Yes" >
                <label>$4.25</label>
            </td>
                            <td title="No" >
                <input id="rubs11" name="rubs11" class="field text small" type="text" value="0"/>
            </td>
                    </tr>
                </tbody>
</table>
4

2 回答 2

0

尽管您在 JS( document.getElementById("mySpan").innerHTML) 中查找它,但您缺少 id 为“mySpan”的元素,因此它出错了。

添加具有该 ID 的 HTML 元素,它应该可以工作。

检查jsfiddle:http: //jsfiddle.net/Chandu/yVaMx/5/

于 2011-01-27T23:31:11.050 回答
0
var theTotal = 0;
for(var j = 0; j < 49; j++)
    theTotal += all[j];

theTotal = all[48];

你为什么做这个?首先计算所有价格的总和,然后仅将最后一个值分配给theTotal。这对我来说似乎是错误的。

于 2011-01-27T23:47:17.843 回答