Skip to content

Instantly share code, notes, and snippets.

@lai32290
Created August 19, 2016 17:57
Show Gist options
  • Save lai32290/8d513521168841217b6e2bd5471beb22 to your computer and use it in GitHub Desktop.
Save lai32290/8d513521168841217b6e2bd5471beb22 to your computer and use it in GitHub Desktop.
A lib to create dynamic form to submit
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