-
Notifications
You must be signed in to change notification settings - Fork 351
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
Comments
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 <select name="multi_test[]" multiple> I am currently working on Using If you have any other questions, let me know. |
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>
@macek Feel free to implement this solution (if you want) |
Thanks @macek! The |
No problem 👍 |
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/
The text was updated successfully, but these errors were encountered: