初始值清除后,如何在输入栏中输入新值?

初始值清除后,如何在输入栏中输入新值?

问题描述:

目标是使用调用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; 


} 

您可以在调用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

,这会很有帮助

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

既然你创建一个四每一行,你可以将要修改的元素的ID添加为隐藏的输入,并使您想要更改文本框编号的字段。当你点击更新时,只需提交表单并在数据库中更新即可。