Fill in form with append and bind
Currently I'm working on a project that has 1 register form. In this form
it is allowed to add multiple members in 1 go. The first member is
required, the others aren't. To add more members I use 'append' from
jQuery that adds the form for the second and third member perfectly.
Now, I want to add a piece of code that fills in my streetname and city
automatically when I fill in the postalcode.
I use jquery with json to fill in some form fields for me. I use this
piece of code:
$('input[name=postcode_1]').change(function(){
//100420//
b_postcode = $('input[name=postcode_1]').val();
$.getJSON('/inc/getPostcodeInfo.php', {postcode:b_postcode},
function(data) {
$('input[name=adres_1]').val('');
$('input[name=plaats_1]').val('');
$.each(data, function(index, array) {
// alert(array['straat']);
$('input[name=adres_1]').val(array['straat']);
$('input[name=plaats_1]').val(array['woonplaats']);
})
});
});
This works perfectly for 1 member but when I add the second and third
member it doesn't work. I read that I'd need to use the function 'bind'
which is what I used for the second and third member like so:
html.bind('change', '[name=postcode_'+ count +']', function(){
b_postcode = $('input[name=postcode_'+ count +']').val();
$.getJSON('/inc/getPostcodeInfo.php', {postcode:b_postcode},
function(data) {
$('input[name=adres_'+ count +']').val('');
$('input[name=plaats_'+ count +']').val('');
$.each(data, function(index, array) {
// alert(array['straat']);
$('input[name=adres_'+ count +']').val(array['straat']);
$('input[name=plaats_'+ count
+']').val(array['woonplaats']);
})
});
});
(Whenever a new member is added count becomes count++)
This seems to only work for the last member added. How can I make this
work so I can automatically fill in the streetname and city for all
members?
Information members:
Member 1 - postalcode: 1111 AA
Member 2 - portalcode: 2222 BB
Member 3 - postalcode: 3333 CC
No comments:
Post a Comment