0

I have a multiple select field where user can select multiple fields. Right now it is taking only the first value of the dropdown.

Here is the jquery code which uses velocity template as front end:

jQuery('select[name="myServices"]').live('change', function(ev) {
            var selectedmyServicesOpts = jQuery(this).find(':selected'),
                    ismyServicesOther=false;
            console.log("Getting all selectedvalue" + selectedmyServicesOpts)
            if(selectedmyServicesOpts.length > 0) {
                jQuery.each(selectedmyServicesOpts, function(index, value) {
                    if(jQuery(value).val() === 'Other (Text field)') {
                        jQuery('.myServicesOther').show();
                        ismyServicesOther=true;
                        return false;
                    }
                });
            }
            if (!ismyServicesOther) {
                jQuery('.myServicesOther').hide();
            }
        });

1 Answer 1

1

For a given <select multiple> element, jQuery("theelement").val() gives an array of selected option values.

In your case, you'd probably want to check:

var values = jQuery(this).val();
if( values.indexOf('Other (Text field)') > -1) {
    jQuery('.myServicesOther').show();
}
else {
    jQuery('.myServicesOther').hide();
}
Sign up to request clarification or add additional context in comments.

4 Comments

Now I can see that this is getting converted as an array , but when I click on the submit button eventually it turns out to be myServices=1st+value&myServices=2nd+value
where as I want them to go as an array
Depending on your server-side language, you will need to use name="myServices[]" in your HTML, and in PHP for example this will give you an array on the server.
No that did not work. I have java on server side though. If you can see i want myService = 1st+value, 2nd+value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.