繁体   English   中英

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

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

目标是使用ON CLICK事件清除字段,该事件调用Java脚本函数clear field(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; 


}

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

这可能会有所帮助

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM