How to copy one array element value into another with jQuery
I have a text input with a name first_name[120]
(predefined key), and I would like to copy the value into another first_name[different predefined key]
field with a click of a button.
There may be other first_name[different predefined key]
fields in the form so I need the flexibility to make this work for the rest. Any suggestions?
EDIT:
Sorry, I think I need to give a little more information.
The form has a list of fields for gift recipient(s) information. There may be multiple gifts listed in the form, with each one having its own fieldset for entering the gift recipient's info. Basically, I am trying to make a "Copy info from above" feature for subsequent gift recipient fields in the form.
EDIT 2:
This is my solution and it works. Please suggest improvements if possible. It is actually bound to a dropdown with a list of all gifts. So the code knows 开发者_开发百科which section to pull the info from and where to drop it based on the id of the dropdown (e.g. 123|130).
$('.copy_info').live('change', function(){
var ids = $(this).val().split('|');
var from = ids[0];
var to = ids[1];
$('#form_fields-' + from + " :input").each(function(){
var name = $(this).attr('name');
var new_name = name.replace(/[\d+]/,to);
$('#form_fields-' + to + ' input[name="' + new_name + '"]').val($(this).val());
});
});
$('button').click(function() {
$('input[name="first_name[xxx]"]').val($('input[name="first_name[120]"]').val());
});
xxx is the number of the first_name key you are copying 120
into.
Also remember you can access the specific button that was clicked with this
or wrap it in a jQuery object with $(this)
to target more specifically the input you are selecting to manipulate.
精彩评论