Javascript提示不断出现

Javascript Prompt keeps appearing

本文关键字:提示 Javascript      更新时间:2023-10-15

在提示中输入值后的前夜,询问"输入盎司数"的提示不断出现。。。。我该怎么做才能删除循环提示。。。我只需要它出现一次。

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

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