如何在清除初始值后将新值输入到输入字段中?

时间:2016-10-02 22:02:24

标签: javascript php html mysql dom

目标是使用ON CLICK事件清除字段,该事件调用java脚本函数clear字段(this.id)clear字段然后可以接收新值。按下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; 


}

3 个答案:

答案 0 :(得分:0)

您可以在调用clearField函数时将新值作为参数传递。然后,不是在函数定义中将innerHTML设置为0,而是可以将innerHTML设置为新值document.getElementById(anId).innerHTML = newValue

答案 1 :(得分:0)

这可能会有所帮助

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

答案 2 :(得分:0)

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