Support multiple select fields in form submissions

This commit was merged in pull request #147.
This commit is contained in:
Robin North
2018-04-12 11:08:59 +01:00
committed by BehindTheMath
parent d3447a95aa
commit 358b6f6836
2 changed files with 33 additions and 8 deletions

View File

@@ -56,7 +56,7 @@
<br />
<label for="get-form-select">Select list:</label>
<select name="select" id="get-form-select">
<select multiple name="select" id="get-form-select">
<option>
Option 1
</option>
@@ -119,7 +119,7 @@
<br />
<label for="post-form-select">Select list:</label>
<select name="select" id="post-form-select">
<select multiple name="select" id="post-form-select">
<option>
Option 1
</option>

View File

@@ -32,18 +32,43 @@ var formAction = function(el, event) {
event.preventDefault()
for (var elementKey in el.elements) {
if (Number.isNaN(Number(elementKey))) {
continue;
}
var element = el.elements[elementKey]
var tagName = element.tagName.toLowerCase()
// jscs:disable disallowImplicitTypeConversion
if (!!element.name && element.attributes !== undefined && element.tagName.toLowerCase() !== "button") {
if (!!element.name && element.attributes !== undefined && tagName !== "button") {
var type = element.attributes.type
// jscs:enable disallowImplicitTypeConversion
if ((!element.attributes.type || element.attributes.type.value !== "checkbox" && element.attributes.type.value !== "radio") || element.checked) {
if ((!type || type.value !== "checkbox" && type.value !== "radio") || element.checked) {
// Build array of values to submit
var values = []
if (tagName === "select") {
var opt
for (var i = 0; i < element.options.length; i++) {
opt = element.options[i]
if (opt.selected) {
values.push(opt.value || opt.text)
}
}
}
else {
values.push(element.value)
}
for (var j = 0; j < values.length; j++) {
options.requestOptions.requestParams.push({
name: encodeURIComponent(element.name),
value: encodeURIComponent(element.value)
value: encodeURIComponent(values[j])
})
}
}
}
}
el.setAttribute(attrState, "submit")