简体   繁体   中英

copy value from one field to another

I have found this fiddle (cannot remember the source): http://jsfiddle.net/FPsdy/1/ which does exactly what I want (copy contents from a field to another)

When I copy it into a php page, though, it doesn't work:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <script type="text/javascript" src="includes/jquery/jquery-1.10.2.min.js"></script>
</head>

<body>

<script type="text/javascript">
$('input[type=submit]').click(function(){
    $('#elm_14').val($('#elm_6').val());
    $('#elm_16').val($('#elm_7').val());
});
</script>

<input type="text" class="input-text " value="" size="32" name="user_data[firstname]" id="elm_6">
<input type="text" class="input-text " value="" size="32" name="user_data[lastname]" id="elm_7"><br />
<input type="submit" value="Continue" name="dispatch[checkout.update_steps]"><br />
<input type="text" class="input-text " value="" size="32" name="user_data[b_firstname]" id="elm_14">
<input type="text" class="input-text " value="" size="32" name="user_data[s_firstname]" id="elm_16">

</body>
</html>

I am sure I have made an amateur mistake but, since I do not know javascript I cannot pinpoint it. Any help is well appreciated

Your js code that attaches the click handler needs to be run AFTER the page loads:

$(function() { // is run after DOM is completely loaded
    $('input[type=submit]').click(function(){
        $('#elm_14').val($('#elm_6').val());
        $('#elm_16').val($('#elm_7').val());
    });
});

In your code, when the JS is run, the submit button doesn't exist yet on the page, so jQuery can't attach the click handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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