最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - echo value from 1 input box into another with jQuery - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Feb 22, 2012 at 17:09 xXPhenom22XxxXPhenom22Xx 1,2755 gold badges30 silver badges63 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

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>   

.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>
发布评论

评论列表(0)

  1. 暂无评论