在清除初始值后,如何在输入字段中输入新值


How can a new value be entered into an input field after the initial value has been cleared?

目标是使用调用java脚本函数clear field(this.id)的ON CLICK事件清除字段,然后清除字段可以接收一个新值。当按下UPDATE按钮时,数据库将使用新值更新。clear field函数清除字段成功,如何输入新值?

<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

这可能会有帮助

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

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