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

标签 javascript php html mysql dom

目标是使用 ON CLICK 事件清除字段,该事件调用 java 脚本函数清除字段(this.id),然后清除字段可以接收新值。当按下 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 设置为新值,而不是在函数定义中将innerHTML 设置为0,如document.getElementById(anId).innerHTML = newValue

关于javascript - 清除初始值后如何在输入字段中输入新值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39822165/

相关文章:

php - 在 PHP (plphp) 中,我们如何使用 Imagick 管理存储在 bytea 中的文件?

javascript - 折叠展开的 html 表格

javascript - jQuery点击后如何保持当前宽度/高度?

javascript - 在特定条件下禁用表中的所有选择元素

javascript - 克隆文档片段

javascript - 尝试创建一个 for 循环,迭代地将其运行的总和加到一个 total 变量中

php检查指定时间是否已过期

php - 将 jQuery datepicker 插入 MySQL 时出现问题

javascript - 当与其关联的元素被禁用时,我可以将 <label> 变灰吗?

javascript - 无法访问 javascript 对象内部数组元素的对象属性