diff --git a/example/forms.html b/example/forms.html index eee4469..df15742 100644 --- a/example/forms.html +++ b/example/forms.html @@ -56,7 +56,7 @@ Select list: - + Option 1 @@ -119,7 +119,7 @@ Select list: - + Option 1 diff --git a/lib/proto/attach-form.js b/lib/proto/attach-form.js index 2ee84a4..ee8ed31 100644 --- a/lib/proto/attach-form.js +++ b/lib/proto/attach-form.js @@ -32,15 +32,40 @@ 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) { - options.requestOptions.requestParams.push({ - name: encodeURIComponent(element.name), - value: encodeURIComponent(element.value) - }) + 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(values[j]) + }) + } } } }