使用 jQuery 将 1 个输入框的值回显到另一个输入框

发布于 2025-01-07 17:35:27 字数 487 浏览 5 评论 0原文

我有一个 AJAX 支持的表单,它更像是一个 5 步向导。在最后提交数据之前不会刷新页面。

在前面的步骤中,一个值被输入到 HTML 文本字段中,我希望在最后一页的另一个文本框中显示相同的值,因为他们在点击发送之前查看提交的最终详细信息。

我认为它需要通过 onChange 事件完成,但我似乎无法让它完全工作,我是一名 PHP 开发人员,所以仍在研究我的 jQuery 技能。这是我编写的代码,但没有成功:

<script type="text/javascript">
$(document).ready(function() {
$("#textbox1").change(function()
        {
    var text = $("#textbox1").val(); 
    $("#textbox2").html(text);
        }); 
});
</script>   

提前感谢先生们。

I have an AJAX powered form which is more like a 5 step wizard. There are no page refreshes until the data is submitted at the very end.

On an earlier step a value is entered into a HTML text field and I would like to have those same values present in another text box on the last page as they review the final details of their submission before they hit send..

I think it needs to be done with an onChange event but I cannot seem to get it to work fully, I am a PHP developer so am still working on my jQuery skills. Here is the code I wrote with no success:

<script type="text/javascript">
$(document).ready(function() {
$("#textbox1").change(function()
        {
    var text = $("#textbox1").val(); 
    $("#textbox2").html(text);
        }); 
});
</script>   

Thanks in advance gents.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月流歌 2025-01-14 17:35:27

你快到了。您只需要使用 val 方法来设置值,而不是 html 方法:

<script type="text/javascript">
$(document).ready(function() {
$("#textbox1").change(function()
        {
    var text = $("#textbox1").val(); 
    $("#textbox2").val(text);
        }); 
});
</script>   

You're almost there. You just need to use val method to set the value, and not html method:

<script type="text/javascript">
$(document).ready(function() {
$("#textbox1").change(function()
        {
    var text = $("#textbox1").val(); 
    $("#textbox2").val(text);
        }); 
});
</script>   
相思碎 2025-01-14 17:35:27

.val 用于获取和设置输入的值:

  • .val() 返回值
  • .val(something) 设置值

所以:

$("#textbox2").val(text);

.html 用于获取/设置元素的 HTML 内容,但对于输入元素来说没有意义。以下是伪造的(这是 .html 正在尝试的):

<input>some html</input>

.val is used for both getting and setting a input's value:

  • .val() returns the value
  • .val(something) sets the value

So:

$("#textbox2").val(text);

.html is used to get/set an element's HTML contents, but for input elements that does not make sense. The following is bogus (which is what .html is trying):

<input>some html</input>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文