Javascript Prompt不断出现

时间:2015-02-12 19:41:33

标签: javascript prompt

询问“输入盎司数”的提示会一直重新出现,在提示中输入一个值之后的前夕....我该怎么做才能删除循环提示...我只需要它出现一次。

<html>
    <body>
        <script type="text/javascript">
            //
            // ******************************
            // Program: LAB3ALT.htm
            // Created by: Tanner DiBella 
            // Date: February 10, 2015
            // Function: Convert Ounces to Pounds
            // *******************************
            //
            /*
            The Ounce to Pounds formula: 1 ounce = 0.0625 pounds
            */

            var i=0;

            while (i<=0) {
                var ounces = prompt("Enter number of Ounces" , "1"); 

                if (ounces==null) { /* Test for cancel */
                    i="1";
                }
                else {
                    var pounds= ounces * 0.0625; /* Compute number of pounds */
                    document.write("<BR>","Ounces : ",ounces);
                    document.write("<BR>","Pounds : ",pounds);
                    document.write("<BR>");
                }
            }
        </script>
    </body>
</html> 

1 个答案:

答案 0 :(得分:2)

else内,您需要设置i>0。那样做。

虽然,这里很混乱。你想让我做什么?更简单的版本是,

while(true){   //It will repeat indefinitely unlesss you break out of it
    var ounces = prompt("Enter number of Ounces: " , "0");
    if (ounces != null) { //Checks if user cancelled, if yes, it reappears
        var pounds= ounces * 0.0625; /* Compute number of pounds */
        document.write("<BR>","Ounces : ",ounces);
        document.write("<BR>","Pounds : ",pounds);
        document.write("<BR>");
        break;  //when criteria is met, you break out of the loop
    }
}