【问题标题】:JavaScript function runs for 1 second then clears?JavaScript 函数运行 1 秒然后清除?
【发布时间】:2013-05-16 11:47:51
【问题描述】:

所以我用一些 JavaScript 函数编写了一个网页。

该页面在 Dreamweaver 中完美运行,但当我在浏览器(Google Chrome 和 Firefox)中试用时,JavaScript 会闪烁一瞬间然后清除。我完全不知道为什么会这样。

<body>
   <form name="myForm">
   <ol>
        <li>
        <h1> TILE CALCULATOR</h1>
        </li>

        <li>

            <label for="wall height">Wall height (cm)</label>
            <input type="text"  id="wall_height" />
        </li>

        <li>
            <label for="wall width">Wall Width (cm)</label>

            <input type="text"  id="wall_width" />
        </li>

        <li>
            <label for="tile height">Tile Height (cm)</label>
            <input type="text"  id="tile_height" />

        </li>
        <li>
            <label for="tile width">Tile Width (cm)</label>
            <input type="text"   id="tile_width" />
        </li>

        <button onclick="javascript:validate();"> Calculate </button>


</ol>
    </form>  

   <br />

<p id="result"></p>
<br />
<canvas id="myCanvas">

Your browser does not support this feature</canvas>
<br />
<br />

<script language="javascript" type="text/javascript">

//functoin to validate the inputs by the user 
//user can only enter a number and all fields must be filled
function validate()
{
    //first make sure canvas is clear
    clearCanvas()
    //take the inputs as variables
    var x = document.getElementById("tile_width").value;
    var y = document.getElementById("tile_height").value;
    var z = document.getElementById("wall_width").value;
    var i = document.getElementById("wall_height").value;
       //check if the user has entered nothing and alert if they have 
       if (x==null || x=="" || y==null || y=="" || z==null || z=="" || i==null || i=="")
    {
        alert("All the fields have to be filled out!");
        clearResult();
    }
    // check if the user has entered invalid values, only numbers can be entered
    if (isNaN(x) == true || isNaN(y) == true || isNaN(z) == true || isNaN(i) == true)
    {
        alert("Dimensions can only be numbers!");
        clearResult();
    }
    //check for negatives
    if (x <= 0 || y <= 0 || z <= 0 || i <= 0)
    {
        alert("invalid dimension input, positive non-zero values only");
        clearResult();
    }
    //if valid calculate tiles and print 
    else
    tileCalculator();
}
function tileCalculator()
{
    //take the input as variables
    var tileWidth = document.getElementById("tile_width").value;
    var tileHeight = document.getElementById("tile_height").value;
    var wallWidth = document.getElementById("wall_width").value;
    var wallHeight = document.getElementById("wall_height").value;
    //find the areas of the tile and the wall
    var tileArea = tileWidth * tileHeight;
    var wallArea = wallWidth * wallHeight;
    //divide these to find the number of tiles needed 
    var noOfTiles = (wallArea/tileArea);
    //prints the result of noOfTiles
    document.getElementById("result").innerHTML=" The number of Tiles you will need are : " + noOfTiles;

    //scalled tiles to the canvas width of your choice
    var scalledWidth = 500;
    var ratioHW = wallHeight/wallWidth;
    var scalledHeight = ratioHW*scalledWidth;
    //scaled tile sizes
    //scale the tiles to correct pixels
    var scalledTileWidth = (tileWidth/wallWidth)*scalledWidth;
    var scalledTileHeight = (tileHeight/wallHeight)*300;
    //finds the number of tiles needs in a row
    var noWidth = wallWidth/tileWidth;
    //number of tiles in a column 
    var noHeight = wallHeight/tileHeight;

    var canvas = document.getElementById("myCanvas");
    canvas.style.width=scalledWidth + "px";
    canvas.style.height=scalledHeight + "px";
    printWall(0,0,noWidth,scalledTileWidth,(scalledTileHeight/2),noHeight);

}

//print a tile given the position and dimensions
function printTile(x,y,tileWidth,tileHeight)
{
    var c=document.getElementById("myCanvas");
    var ctx=c.getContext("2d");
    ctx.fillRect(x,y,tileWidth,tileHeight);
}
//prints one row of tiles given the starting position and number to be printed
function printTileRow(x,y,numberOfTiles,tileWidth,tileHeight)
{
    var start = 0;
    //loops upto number of tiles in a row 
    while (start < numberOfTiles)
    {
        //prints a tile each time
        printTile(x,y,tileWidth,tileHeight);

        //next brick position
        x = x + (tileWidth + 1); //  add a space between tiles here.
        start++;
    }
}

//prints the wall 
function printWall(x,y,numberOfTiles,tileWidth,tileHeight,numberOfRows)
{   
    //holds whether last row was shifted
    var shiftCount = 0; 
    //starting index
    var start = 0;
    //loop up adding a row until required number of rows
    while (start < numberOfRows)

    { 
       //prints half a tile at the start of each row
       printTile(0,y,(0.5 * tileWidth - 1),(tileHeight));
       //prints the row 
       printTileRow((x+shiftCount),y,numberOfTiles,tileWidth,tileHeight);
       //if shifted
       if (shiftCount > 0)
       {
           //was shifted last row
           shiftCount = 0;
       }
       else
       {
           //was not shifted last row
           shiftCount = shiftCount + (0.5*tileWidth);
       }
       start++;
       //start next row
       y = y + (tileHeight + 1);
    }
}

//clears the canvus each time the button is pressed
function clearCanvas()
{
    //reset canvus to 300 by 300 and clear
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext('2d');
    canvas.style.height="300px";
    canvas.style.width="300px";
    context.clearRect(0,0,300,300);
}
function clearResult()
{
    document.getElementById("result").innerHTML="";
}

</script> 
</body>

如果有人能快速找到我,我将不胜感激!谢谢,

【问题讨论】:

  • 您的 javascript 的计划行为是什么?
  • 它在画布上打印出一堵墙,并给出该区域的结果。它打印墙并在dreamweaver中给出结果然后停留在那里,在浏览器中打印并给出正确的结果,但只会在屏幕上闪烁一秒钟

标签: javascript html


【解决方案1】:

尝试在表单标签而不是按钮标签上使用onclick="javascript:validate();",并尝试使用“onsubmit”而不是“onclick”

【讨论】:

    【解决方案2】:

    只需将您的 onclick 属性替换为 onclick='validate(); return false'

    OR(比以前更好)

    一旦 HTML5 按钮的默认行为是提交表单,只需将 type="button" 属性添加到您的按钮标签即可。

    实用提示

    以编程方式绑定您的处理程序,这种方式(没有 jQuery):

            <button type="button" id="calculate"> Calculate </button>
    
    ...
    
            window.addEventListener("load", function(){
              document.getElementById("calculate").addEventListener("click", validate);
            });
    

    【讨论】:

      【解决方案3】:

      将按钮保留在表单中没有问题,您每次都需要返回 false 以避免使用这样的 return false 提交表单

          onclick="javascript:validate();return false;"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-04
        • 2019-07-08
        • 2015-08-28
        • 1970-01-01
        • 2023-04-05
        • 2019-07-31
        相关资源
        最近更新 更多