2013-05-16 78 views
0

所以我寫了一個帶有幾個JavaScript函數的網頁。JavaScript函數運行1秒後清除?

頁運行完全在Dreamweaver但是當我在瀏覽器(谷歌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> 

我真的很感激,如果有人可以快速找我!謝謝,

+0

你的javascript的計劃行爲是什麼? – Rudy

+0

它在畫布上打印出一面牆磚並給出該區域的結果。它打印牆壁,並在Dreamweaver中給出結果,然後停留在那裏,在瀏覽器中打印它並給出正確的結果,但僅在屏幕上閃爍一秒 –

回答

1

嘗試的形式,而不是標籤上的按鈕標籤使用onclick="javascript:validate();"並嘗試使用「的onsubmit」而不是「的onclick」

+0

謝謝!我意識到它沒有工作,因爲我的按鈕是在窗體中! –

+0

這是正確的答案 –

0

僅僅通過更換onclick='validate(); return false'您的onclick屬性。

OR(優於先前)

一個type="button"屬性只是添加到您的按鈕標籤,一旦HTML5按鈕的默認行爲是提交表單。

有用的提示

綁定處理程序編程,這樣(沒有的jQuery):

 <button type="button" id="calculate"> Calculate </button> 

... 

     window.addEventListener("load", function(){ 
      document.getElementById("calculate").addEventListener("click", validate); 
     }); 
0

是沒有問題的形式kepping的按鈕,你就需要返回false每次避免使用這種形式提交返回false這樣的

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