简体   繁体   English

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

[英]echo value from 1 input box into another with jQuery

I have an AJAX powered form which is more like a 5 step wizard. 我有一个AJAX支持的表单,它更像是5步向导。 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.. 在较早的步骤中,将一个值输入到HTML文本字段中,我希望在最后一页的另一个文本框中显示相同的值,因为他们在单击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. 我认为这需要通过onChange事件来完成,但我似乎无法使其完全正常工作,我是PHP开发人员,因此仍在研究jQuery技能。 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. 在此先感谢您。

You're almost there. 你快到了。 You just need to use val method to set the value, and not html method: 您只需要使用val方法来设置值,而不是html方法:

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

.val is used for both getting and setting a input's value: .val用于获取和设置输入值:

  • .val() returns the value .val()返回值
  • .val(something) sets the value .val(something)设置值

So: 所以:

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

.html is used to get/set an element's HTML contents, but for input elements that does not make sense. .html用于获取/设置元素的HTML内容,但是对于没有意义的输入元素。 The following is bogus (which is what .html is trying): 以下是伪造的( .html正在尝试的):

<input>some html</input>

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

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