ajax - JQuery concatenate values as they are selected from a dropdown list -
the idea user selects users dropdown list send them email. when new user selected, email address of user needs concatenated others using format: 'user1@mail.com','user2@mail.com','user3@mail.com'
format needed because use django , format recipient list
.
right can bring email address of user selected using ajax
can´t concatenate in way need, have:
forms.py class myform(forms.form): userslist = forms.modelchoicefield(queryset = user.objects.filter(here goes condition show users), empty_label='list of users', label='users', required=false) emailaddress = forms.charfield(max_length=1000, label='send to:', required=false) comment = forms.charfield(widget=ckeditoruploadingwidget(), label="comment:")
now ajax view
views.py class mails(templateview): def get(self, request, *args, **kwargs): id_user = request.get['id'] = user.objects.filter(id = id_user) data = serializers.serialize('json', us, fields=('email')) return httpresponse(data, content_type='application/json')
and jquery:
<script> $('#id_userlist').on('change', concatenate); function concatenate() { var id = $(this).val(); $.ajax({ data: { 'id': id }, url: '/the_url_to_get_data/', type: 'get', success: function (data) { var mail = "" (var = 0; < data.length; i++) { mail += data[i].fields.email $('#id_emailaddress').val(mail) } } }) } </script>
how can concatenate values in nedded format?
if need more info please let me know.
use .join
concatenate array elements separator. , use .map
extract email each element of data
.
success: function(data) { var mail = data.map(function(el) { return el.fields.email; }).join(','); $("#id_emailaddress").val(email); }
Comments
Post a Comment