IE non supporta ancora l'attributo HTML5 form
per l'elemento <input>
o <button>
.
Se si desidera associare un elemento di input esterno con la forma, è possibile duplicare un ' "ombra", come un campo di ingresso invisibile all'interno del vostro modulo, e allegare un gestore di eventi all'evento del form onsubmit
. Quando l'utente invia il modulo, aggiorna il valore all'interno.
Il seguente polyfill (richiede jQuery) emula la funzione. Rende gli elementi di ingresso con form
attributo si comportano come se fossero all'interno del modulo:
(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) {
var $form = $(this);
// serialize data
var data = $('[form='+ $form.attr('id') + ']').serializeArray();
// append data to form
$form.appendField(data);
}).each(function() {
var form = this,
$form = $(form),
$fields = $('[form=' + $form.attr('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);
versione live: http://jsfiddle.net/hbxk4e61/
Tra l'altro, è possibile controllare this page per verificare quanti HTML5 presenta il browser attualmente supporta. Ad esempio, sto utilizzando Chrome 31 e supporta questo attributo.
possibile duplicato del [Un modo per sostenere/spessore attributo form pulsante in IE10?] (Http://stackoverflow.com/questions/16694195/any-way-to-support-shim-button -form-attribute-in-ie10) –
Come indicano le risposte, IE non supporta questo. Ma puoi votare per cambiare: https://wpdev.uservoice.com/forums/257854-microsoft-edge-developer/suggestions/7327649-add-support-for-the-form-attribute – jmoreno
Possibile duplicato di [polyfill html5 input "form" attributo] (http://stackoverflow.com/questions/17742275/polyfill-html5-input-form-attribute) – taber