Fix form submission (#129)

* Fix check for radio and checkbox inputs

* Fix GET form submission

* Add example forms for testing

* Refactor query string building
This commit was merged in pull request #129.
This commit is contained in:
Robin North
2018-03-02 20:25:08 +00:00
committed by GitHub
parent 0c7af354fd
commit 07baae8e4d
8 changed files with 214 additions and 38 deletions

View File

@@ -11,7 +11,8 @@ var formAction = function(el, event) {
// Initialize requestOptions
options.requestOptions = {
requestUrl: el.getAttribute("action") || window.location.href,
requestMethod: el.getAttribute("method") || "GET"
requestMethod: el.getAttribute("method") || "GET",
requestParams: []
}
// create a testable virtual link of the form action
@@ -44,24 +45,20 @@ var formAction = function(el, event) {
event.preventDefault()
var paramObject = []
for (var elementKey in el.elements) {
var element = el.elements[elementKey]
// jscs:disable disallowImplicitTypeConversion
if (!!element.name && element.attributes !== undefined && element.tagName.toLowerCase() !== "button") {
// jscs:enable disallowImplicitTypeConversion
if ((element.attributes.type !== "checkbox" && element.attributes.type !== "radio") || element.checked) {
paramObject.push({name: encodeURIComponent(element.name), value: encodeURIComponent(element.value)})
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)
})
}
}
}
// Creating a getString
var paramsString = (paramObject.map(function(value) {return value.name + "=" + value.value})).join("&")
options.requestOptions.requestPayload = paramObject
options.requestOptions.requestPayloadString = paramsString
el.setAttribute(attrClick, "submit")
options.triggerElement = el