2016-10-02 53 views
0

目標是使用調用java腳本函數清除字段(this.id)的ON CLICK事件清除字段清除字段然後可以接收新值。當按下UPDATE按鈕時,數據庫用新值更新。清除現場功能成功清除現場,如何輸入新值?初始值清除後,如何在輸入欄中輸入新值?

<html> 
     <head> 
      <script src="JavaScript/getVal.js" type="text/javascript"></script> 
     </head> 
     <body> 
      <a href="poll.html">Poll</a> 
      <?php 
      $connection = mysqli_connect("localhost", "root", ""); 
      mysqli_select_db($connection, "ukelection2015"); 
      $rs = mysqli_query($connection, "select * from poll"); 
      ?> 
      <hr> 
      <table border=1 id="myTable"> 
       <tr> 
        <th>Name</th> 
        <th>Value</th> 
        <th>Color</th> 
        <th></th> 

        <th> </th> 
       </tr> 
       <?php 
       $id = 0; 
       while ($row = mysqli_fetch_array($rs)) { 

        print("<form method= 'post' action= 'updatePoll.php'>"); 
        for ($x = 0; $x <= 2; $x++) { 
         if ($x == 0) { 
          print("<tr>"); 
         } else { 

          print("<td>" . $row['name'] . "</td>" . "<td id= '".$id."' value = '" . $row['value'] . "' onclick = 'clearField(this.id)'>" . $row['value'] . "</td>" . "<td>" . $row['color'] . "</td>" . "<td><a href = ''>Update</a></td>"); 

          $x++; 
          if ($x == 1) { 
           print "</tr>"; 
          } 
          $id++; 
         } 
        } 

        // this hidden field is used in updatePoll.php to get the party name to update (i.e. $name=$_POST['name'];) 
       /* print("<input type='hidden' name ='name' value={$row['name']}>"); */ 

        print("<tr>"); 

        // name 
        // value 
        // color 

       /* print("<td><input type='submit' value='Update' onclick='alertMsg()'/></td>"); */ 
        print("</tr>"); 
        print("</form>"); 
       } 
       mysqli_close($connection); 
       ?> 

      </table> 
     </body> 
    </html> 

     This is the javascript function 
/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 


function clearField(anId) { 
    document.getElementById(anId).innerHTML = 0; 


} 

回答

0

您可以在調用clearField函數時將新值作爲參數傳遞。然後,而不是在函數定義的innerHTML設置爲0,可以設置innerHTML來新的價值document.getElementById(anId).innerHTML = newValue

+0

謝謝你們,你們指出了我的正確方向。我想我已經找到了答案。這是一個html5屬性; contenteditable ='true'我插入的內容如下 – Tony75

+0

print(「​​」。$ row ['name']。「」。「「。$ row ['value']。」「。」​​「。$ row ['color']。」「。」​​Update「); 現在可以更改單元格內容。接下來的一點是使用UPDATE按鈕來獲取新值並用這個新值更新數據庫。我認爲可以修改clearField函數來提供此功能。所有的建議歡迎 – Tony75

0

,這會很有幫助

<script type="text/javascript> 
function clearField(anId) { 
    document.getElementById(anId).innerHTML = 0; 
} 
</script> 
0

既然你創建一個四每一行,你可以將要修改的元素的ID添加爲隱藏的輸入,並使您想要更改文本框編號的字段。當你點擊更新時,只需提交表單並在數據庫中更新即可。