admin管理员组

文章数量:1200974

I have two forms.

I want to copy the value of one of the text fields in form 1 into another in form 2.

Text field names and ids are different.

How can I achieve this?

This didn't work:

document.getElementById('name').value = document.getElementById('user').value;

Thanks!

I have two forms.

I want to copy the value of one of the text fields in form 1 into another in form 2.

Text field names and ids are different.

How can I achieve this?

This didn't work:

document.getElementById('name').value = document.getElementById('user').value;

Thanks!

Share Improve this question asked Feb 25, 2013 at 16:10 Claudio DelgadoClaudio Delgado 2,3497 gold badges21 silver badges27 bronze badges 2
  • 1 Are you using jQuery or not - because thats vanilla js youve posted in your question? – prodigitalson Commented Feb 25, 2013 at 16:12
  • Check for duplicate ID, that is invalid and would have the potential to make the above fail. – Mark Schultheiss Commented Feb 25, 2013 at 16:16
Add a comment  | 

3 Answers 3

Reset to default 17

If you're asking for jQuery you could try:

$("#name").val($("#user").val());

http://jsbin.com/exudif/2/

$(document).ready(function()
{
    $('#btn1').click(function()
    {
         $('#field2').val($('#field1').val());
    });
});

To get the value using jquery .it has to be done this way . Also make sure you have given id to the input fields .

like this: <input type='text' name='user' id='user' >

Then only this code will work properly

$(document).ready(function()
{
    $('#buttonElement').click(function()
    {
         $('#name').val($('#user').val());
    });
});

本文标签: