-1

这是我的Javascript...

<script type="text/javascript">

    function validator() {
         var x = parseInt(document.MyForm.textnuming.value);
         alert(x);


        if (document.getElementById('rbsm').checked) {
            if(x == 0){
                var y = 7.70;
            }
            else if(x == 1){
                var y = 8.41;
            }
            else if(x == 2){
                var y = 9.10;
            }
            else if(x == 3){
                var y = 9.65;
            }
            else{
                var y = 9.65+(0.71*x);
            }
        }


        else if (document.getElementById('rbme').checked || document.getElementById('rbla').checked) {
            if(x == 0){
                var y = 13.84;
            }
            else if(x == 1){
                var y = 14.95;
            }
            else if(x == 2){
                var y = 15.99;
            }
            else if(x == 3){
                var y = 17.04;
            }
            else{
                var y = 17.04+(1.09*x);
            }
        }


        else if (document.getElementById('rbmi').checked) {
            if(x < 3){
                var y = 3.99;
            }
            else{
                var y = 4.28;
            }
        }
        else {
            alert("Error!");
        }

        document.getElementById(testing).innerHTML = y;





}




</script>

下面是我的 HTML 表单...

<body>
<form name="MyForm">
    <input type="radio" name="rbsize" value="small" id="rbsm" />Small<br />
    <input type="radio" name="rbsize" value="medium" id="rbme" />Medium<br />
    <input type="radio" name="rbsize" value="large" id="rbla" />Large<br />
    <input type="radio" name="rbsize" value="mini" id="rbmi" />Mini<br />
    Number of Ingredients: <input type="text" name="textnuming"  width="2" maxlength="2" />
    <input type="button" value="Press me!" onClick="validator()" />

</form>
<div id="testing">Price!</div>




</body>

除了最后一部分之外,我的脚本已成功执行。我希望 div“测试”在通过脚本后说出 var y 是什么,但它不会改变..

4

2 回答 2

2

You should do something like this:

document.getElementById('testing').innerHTML = '' + y;

Without quotes, testing is trying to reference a variable called testing instead of getting the <div> with id='testing'.

于 2012-04-07T01:26:13.530 回答
0

last sentence is wrong

should be: document.getElementById("testing").innerHTML = y;

于 2012-04-07T01:28:11.490 回答