2017-05-03 65 views
-1

所以我做了爲p段落領域的地位,它應該是能夠保持按鈕點擊後的值,但它只會出現短暫的和消失現場更新則在前看不見

庫JS只是有填寫所需的數據陣列所需

<script src="library.js"></script> 
<b id="chakra"></b> 
<div id="planet"></div> 
<script>  
    var index = setChakra(0); 
    var planetConection = ""; 

    function setChakra(index){ 
     document.getElementById("chakra").innerHTML = chakra[index]; 
     while (index < chakra.length){ 
      getPlanets(index); 
      if (index < chakra.length-1) index++; 
      else index = 0; 
      break; 
     } 
     return index; 
    } 

    function getPlanets(chakra){ 
     var planetIndex = 0; 
     document.getElementById("planet").innerHTML = ""; 
     while(planetIndex < chakraPlanets[chakra].length){ 
       document.getElementById("planet").innerHTML = document.getElementById("planet").innerHTML + planetDesc[chakraPlanets[chakra][planetIndex]] + 
       "<form>" + 
        "<input id=\"planetStatus" + planetIndex + "\" type=\"text\" name=\"plntStat\">" + 
        "<button onclick=\"getPlanetConection(" + planetIndex + ")\">Click Me!</button>" + 
       "</form>" + 
       "<p id=\"status\"></p>"; 
       planetIndex++; 
      } 
    } 

    function getPlanetConection(planetIndex){ 
     planetConection = document.getElementById("planetStatus" + planetIndex).value; 
     document.getElementById("status").innerHTML = planetConection; 
    } 

</script> 
<button onclick = "index = setChakra(index)" >Click Me!</button> 

回答

0

好的解決它的按鈕,就是要輸入如果我沒有刷新頁面

<script src="library.js"></script> 
<b id="chakra"></b> 
<div id="planet"></div> 
<script>  
    var index = setChakra(0); 
    var planetConection = ""; 

    function setChakra(index){ 
     document.getElementById("chakra").innerHTML = chakra[index]; 
     while (index < chakra.length){ 
      getPlanets(index); 
      if (index < chakra.length-1) index++; 
      else index = 0; 
      break; 
     } 
     return index; 
    } 

    function getPlanets(chakra){ 
     var planetIndex = 0; 
     document.getElementById("planet").innerHTML = ""; 
     while(planetIndex < chakraPlanets[chakra].length){ 
      document.getElementById("planet").innerHTML = document.getElementById("planet").innerHTML + planetDesc[chakraPlanets[chakra][planetIndex]] + 
      "<form>" + 
       "<input id=\"planetStatus" + planetIndex + "\" type=\"text\" name=\"plntStat\">" + 
       "<input value=\"Click\" type=\"button\" onclick=\"getPlanetConection(" + planetIndex + ")\"/>" + 
      "</form>" + 
      "<p id=\"status" + planetIndex + "\"></p>"; 
      planetIndex++; 
     } 
    } 

    function getPlanetConection(planetIndex){ 
     planetConection = document.getElementById("planetStatus" + planetIndex).value; 
     document.getElementById("status" + planetIndex).innerHTML = planetConection; 
    } 
</script> 
<button onclick = "index = setChakra(index)" >Click Me!</button> 
0

我想在頁面重新加載。添加一個值爲'button'的類型屬性給你的按鈕。 像這樣:

<button type="button" onclick = "index = setChakra(index)" >Click Me!</button> 

"<button type=\"button\" onclick=\"getPlanetConection(" + planetIndex + ")\">Click Me!</button>" 

這使得該按鈕用作按鈕,而不是一個提交按鈕。