Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <input type="text" form="myform" name="inp1" />
- <form id="myform" name="myform">
- ...
- </form>
- (function($) {
- /**
- * polyfill for html5 form attr
- */
- // detect if browser supports this
- var sampleElement = $('[form]').get(0);
- var isIE11 = !(window.ActiveXObject) && "ActiveXObject" in window;
- if (sampleElement && window.HTMLFormElement && sampleElement.form instanceof HTMLFormElement && !isIE11) {
- // browser supports it, no need to fix
- return;
- }
- /**
- * Append a field to a form
- *
- */
- $.fn.appendField = function(data) {
- // for form only
- if (!this.is('form')) return;
- // wrap data
- if (!$.isArray(data) && data.name && data.value) {
- data = [data];
- }
- var $form = this;
- // attach new params
- $.each(data, function(i, item) {
- $('<input/>')
- .attr('type', 'hidden')
- .attr('name', item.name)
- .val(item.value).appendTo($form);
- });
- return $form;
- };
- /**
- * Find all input fields with form attribute point to jQuery object
- *
- */
- $('form[id]').submit(function(e) {
- // serialize data
- var data = $('[form='+ this.id + ']').serializeArray();
- // append data to form
- $(this).appendField(data);
- }).each(function() {
- var form = this,
- $fields = $('[form=' + this.id + ']');
- $fields.filter('button, input').filter('[type=reset],[type=submit]').click(function() {
- var type = this.type.toLowerCase();
- if (type === 'reset') {
- // reset form
- form.reset();
- // for elements outside form
- $fields.each(function() {
- this.value = this.defaultValue;
- this.checked = this.defaultChecked;
- }).filter('select').each(function() {
- $(this).find('option').each(function() {
- this.selected = this.defaultSelected;
- });
- });
- } else if (type.match(/^submit|image$/i)) {
- $(form).appendField({name: this.name, value: this.value}).submit();
- }
- });
- });
- })(jQuery);
- (function($) {
- /**
- * polyfill for html5 form attr
- */
- // detect if browser supports this
- var SAMPLE_FORM_NAME = "html-5-polyfill-test";
- var sampleForm = $("<form id='" + SAMPLE_FORM_NAME + "'/>");
- var sampleFormAndHiddenInput = sampleForm.add($("<input type='hidden' form='" + SAMPLE_FORM_NAME + "'/>"));
- sampleFormAndHiddenInput.prependTo('body');
- var sampleElementFound = sampleForm[0].elements[0];
- sampleFormAndHiddenInput.remove();
- if (sampleElementFound) {
- // browser supports it, no need to fix
- return;
- }
- /**
- * Append a field to a form
- *
- */
- $.fn.appendField = function(data) {
- // for form only
- if (!this.is('form')) return;
- // wrap data
- if (!$.isArray(data) && data.name && data.value) {
- data = [data];
- }
- var $form = this;
- // attach new params
- $.each(data, function(i, item) {
- $('<input/>')
- .attr('type', 'hidden')
- .attr('name', item.name)
- .val(item.value).appendTo($form);
- });
- return $form;
- };
- /**
- * Find all input fields with form attribute point to jQuery object
- *
- */
- $('form[id]').submit(function(e) {
- // serialize data
- var data = $('[form='+ this.id + ']').serializeArray();
- // append data to form
- $(this).appendField(data);
- }).each(function() {
- var form = this,
- $fields = $('[form=' + this.id + ']');
- $fields.filter('button, input').filter('[type=reset],[type=submit]').click(function() {
- var type = this.type.toLowerCase();
- if (type === 'reset') {
- // reset form
- form.reset();
- // for elements outside form
- $fields.each(function() {
- this.value = this.defaultValue;
- this.checked = this.defaultChecked;
- }).filter('select').each(function() {
- $(this).find('option').each(function() {
- this.selected = this.defaultSelected;
- });
- });
- } else if (type.match(/^submit|image$/i)) {
- $(form).appendField({name: this.name, value: this.value}).submit();
- }
- });
- });
- })(jQuery);
- var isEdge = navigator.userAgent.indexOf("Edge");
- if (sampleElement && window.HTMLFormElement && sampleElement.form instanceof HTMLFormElement && !isIE11 && isEdge == -1) {
- // browser supports it, no need to fix
- return;
- }
- (function($) {
- /**
- * polyfill for html5 form attr
- */
- // detect if browser supports this
- var SAMPLE_FORM_NAME = "html-5-polyfill-test";
- var sampleForm = $("<form id='" + SAMPLE_FORM_NAME + "'/>");
- var sampleFormAndHiddenInput = sampleForm.add($("<input type='hidden' form='" + SAMPLE_FORM_NAME + "'/>"));
- sampleFormAndHiddenInput.prependTo('body');
- var sampleElementFound = sampleForm[0].elements[0];
- sampleFormAndHiddenInput.remove();
- if (sampleElementFound) {
- // browser supports it, no need to fix
- return;
- }
- /**
- * Append a field to a form
- *
- */
- var CLASS_NAME_POLYFILL_MARKER = "html-5-polyfill-form-attr-marker";
- $.fn.appendField = function(data) {
- // for form only
- if (!this.is('form')) return;
- // wrap data
- if (!$.isArray(data) && data.name && data.value) {
- data = [data];
- }
- var $form = this;
- // attach new params
- $.each(data, function(i, item) {
- $('<input/>')
- .attr('type', 'hidden')
- .attr('name', item.name)
- .attr('class', CLASS_NAME_POLYFILL_MARKER)
- .val(item.value).appendTo($form);
- });
- return $form;
- };
- /**
- * Find all input fields with form attribute point to jQuery object
- *
- */
- $('form[id]').submit(function(e, origSubmit) {
- // clean up form from last submit
- $('.'+CLASS_NAME_POLYFILL_MARKER, this).remove();
- // serialize data
- var data = $('[form='+ this.id + ']').serializeArray();
- // add data from external submit, if needed:
- if (origSubmit && origSubmit.name)
- data.push({name: origSubmit.name, value: origSubmit.value})
- // append data to form
- $(this).appendField(data);
- })
- //submit and reset behaviour
- $('button[type=reset], input[type=reset]').click(function() {
- //extend reset buttons to fields with matching form attribute
- // reset form
- var formId = $(this).attr("form");
- var formJq = $('#'+formId);
- if (formJq.length)
- formJq[0].reset();
- // for elements outside form
- if (!formId)
- formId = $(this).closest("form").attr("id");
- $fields = $('[form=' + formId + ']');
- $fields.each(function() {
- this.value = this.defaultValue;
- this.checked = this.defaultChecked;
- }).filter('select').each(function() {
- $(this).find('option').each(function() {
- this.selected = this.defaultSelected;
- });
- });
- });
- $('button[type=submit], input[type=submit], input[type=image]').click(function() {
- var formId = $(this).attr("form") || $(this).closest("form").attr("id");
- $('#'+formId).trigger('submit', this); //send clicked submit as extra parameter
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement