0
<html>
<head>Testing</head>
<body>
    <?php
        if(isset($_POST['postbtn'])){
            echo "<script>alert('I am here')</script>";
            echo "<script>document.getElementById('txt_name').value='edit'</script>";
        }
    ?>

    <div id="wrap">
        <form id="data_entry" method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
            <div class="rowbox">
                <input type="text" class="textbox" id="txt_name" value="">
            </div>

            <div class="btn">
                <input type="submit" id="postme" name="postbtn">
            </div>
        </form>
    </div>
</body>
</html>

我是 javascript 和 php 的新手,我需要知道的是在提交表单后我可以看到警报消息“我在这里”,但我没有看到我在文本字段中使用 document.getElementById 发布的值对于 html 表单。为什么?

谢谢

4

1 回答 1

2

由于该元素尚未加载,请将您的脚本移动到正文的末尾。

<?php
    if(isset($_POST['postbtn'])){
        echo "<script>alert('I am here')</script>";
        echo "<script>document.getElementById('txt_name').value='edit';</script>";
    }
?>
</body>
</html>
于 2013-10-19T07:09:33.010 回答