Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returns single string value for multi-select elements #41

Closed
aaemnnosttv opened this issue Oct 28, 2014 · 4 comments
Closed

Returns single string value for multi-select elements #41

aaemnnosttv opened this issue Oct 28, 2014 · 4 comments
Labels

Comments

@aaemnnosttv
Copy link

The returned object contains only a single value for a <select multiple> with multiple values selected.
The expected value would be an array with all selected values.

http://jsfiddle.net/9hwvowkx/

@macek macek added the support label Oct 29, 2014
@macek
Copy link
Owner

macek commented Oct 29, 2014

Thanks for posting a fiddle. It helps me answer these questions a lot easier.

Your select

<select name="multi_test" multiple>

Update the name to multi_test[]

<select name="multi_test[]" multiple>

I am currently working on 3.x which will fully implement W3C HTML JSON form submission spec as discussed in #24.

Using 3.x you will be able to do <select name="foo" multiple> and get an array of results.

If you have any other questions, let me know.

@lextas
Copy link

lextas commented Nov 4, 2014

I was facing the same problem. The problem I had was that when I used the square brackets on the name attribute, my MVC model binding did no longer work.

For those who are experiencing the same issue you can use the following solution:

        function addPair(pair) {

            if (!patterns.validate.test(pair.name)) return this;
            var obj = makeObject(pair.name, encode(pair));

            /* modification to store multi select values */
            // check if key already exists
            if (pair.name in data) {

                // get current value
                var value = data[pair.name];

                if (!$.isArray(value)) {
                    // convert existing value to array
                    value = value.split(',');
                }

                // reset original value to prevent duplicates
                data[pair.name] = null;

                // push new value to the (newly created) array
                value.push(pair.value);

                // build new object                        
                obj = build([], pair.name, value);
            }
            /* /end modification */

            data = helper.extend(true, data, obj);

            return this;
        }

What it does: it checks if the given key already exists. If so, it turns the value in an array and pushes the new value into that. If the value is already an array it just pushes it.

This code works with the following HTML markup

    <select name="multitest" multiple="multiple">
        <option value="x">X</option>
        <option value="y">Y</option>
        <option value="z">Z</option>
    </select>
  • Notice the name attribute doesn't need the square brackets.

@macek Feel free to implement this solution (if you want)

@aaemnnosttv
Copy link
Author

Thanks @macek! The [] workaround is pretty painless and works just fine. Thanks!

@macek
Copy link
Owner

macek commented Jul 8, 2015

No problem 👍

@macek macek closed this as completed Jul 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants