Created
August 19, 2016 17:57
-
-
Save lai32290/8d513521168841217b6e2bd5471beb22 to your computer and use it in GitHub Desktop.
A lib to create dynamic form to submit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function DynamicForm(url) | |
{ | |
var self = this; | |
var fields = []; | |
self.target = ""; | |
self.method = "get"; | |
self.action = url; | |
self.addField = function(name, value) { | |
fields.push({name: name, value: value}); | |
return self; | |
}; | |
self.remoteField = function(name) { | |
fields = fields.filter(function(item) { | |
return item.name != name; | |
}); | |
return self; | |
}; | |
self.getFields = function() { | |
return fields; | |
}; | |
} | |
DynamicForm.prototype.submit = function submit () { | |
var form = document.createElement("form"); | |
form.action = this.action; | |
form.method = this.method; | |
form.target = this.target; | |
this.getFields().forEach(function(item) { | |
var field = document.createElement("input"); | |
field.type = "hidden"; | |
field.name = item.name; | |
field.value = item.value; | |
form.appendChild(field); | |
}); | |
form.submit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment