0

我在 PHP 中有一个如下所示的表单:

<?php
   $txtVal1 = "value1";
?>
<script type="text/javascript">
 function post_data(){
   alert(document.getElementById("text1").value);
 }
</script>
<form method="post" action="">
<input type="text" value="<?php echo $txtVal1;?>" id="text1"/>
<input type="button" value="Post" onclick="post_data()"/>
</form>

我的问题是,每当我单击“发布”按钮时,我都会收到一条警报消息,即“value1”。这可以。但是现在,如果我将文本框的值从 UI 更改为其他值,我仍然会得到旧值。是否有任何解决方案来获取更改的值?

4

4 回答 4

0

您的页面在加载第一个 php 值分配时,然后运行所有值,这就是问题所在。您更改为上述一项分配值$txtVal1 = "value1";

于 2012-12-31T06:54:22.073 回答
0

试试这个。这行得通。检查您的 php 值是否正确放置值

    <script type="text/javascript">
 function post_data(){
   alert(document.getElementById("text1").value);

 }
</script>

<form method="post" action="">
<input type="text" id="text1"/>
<input type="button" value="Post" onclick="post_data()"/>
</form>
于 2012-12-31T06:59:22.717 回答
0
<pre>
<?php <br/>
   $txtVal1 = "value1";<>
?>
<script type="text/javascript">
 function post_data(){
   alert(document.getElementById("text1").value);
 }

 function test(val){
    document.getElementById("text1").value = val;
 }
</script>
<form method="post" action="">
<input type="text1" value="<?php echo $txtVal1;?>" onChange="test(this.value)" id="text1"/>
<input type="button" value="Post" onclick="post_data()"/>
</form>
于 2012-12-31T06:59:37.420 回答
0

试试这个jQuery 小提琴

这可能是矫枉过正,但它的工作原理。StackOverflow 不喜欢指向 jsfiddle 的链接?

$("#submit").click(function(){
    alert($("#text1").val());
});
于 2012-12-31T07:15:08.593 回答