使用 jQuery 将 1 个输入框的值回显到另一个输入框
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你快到了。您只需要使用
val
方法来设置值,而不是html
方法:You're almost there. You just need to use
val
method to set the value, and nothtml
method:.val
用于获取和设置输入的值:.val()
返回值.val(something)
设置值所以:
.html
用于获取/设置元素的 HTML 内容,但对于输入元素来说没有意义。以下是伪造的(这是.html
正在尝试的):.val
is used for both getting and setting a input's value:.val()
returns the value.val(something)
sets the valueSo:
.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):