[jQuery] form() plugin revisited

[jQuery] form() plugin revisited

Here's yet another cut at a form submission plugin. This one
has two important qualities; it gathers up the name/value
pairs in strict page order and only provides the submit
button that was actually clicked. This version seems to be
exactly the same as a non-ajax form submission, as far as
a PHP backend is concerned anyway.
http://markc.renta.net/jquery/form_test.html
Usage: $('#form').form('#target');
The idea of the "evaljs" submit button is so that one option
is available that does not redraw over the target, ie; when
checking input variables then a warning can be returned
somewhere else on the page other than the default target ID.
It's only had minimal testing with FF 1.5 and Konqueror does
not seem to recognize event.explicitOriginalTarget so I suspect
it's a Firefox-only DOM event.
Does anyone know of a cross-browser way to detect which submit
button has been clicked ?
$.fn.form = function(target, callback){
return this.submit(function(e){
var a = [];
var doeval = false;
e.preventDefault();
// e.stopPropagation()
for (i = 0; i < this.childNodes.length; i++) {
var o = this.childNodes[i];
if (o.tagName != 'INPUT'
&& o.tagName != 'SELECT'
&& o.tagName != 'TEXTAREA') { continue; }
if ((o.type == 'checkbox'
|| o.type == 'radio') && !o.checked) { continue; }
if (e.explicitOriginalTarget) { // probably FF only
if (o.type == 'submit'
&& o.name != e.explicitOriginalTarget.name) { continue; }
if (e.explicitOriginalTarget.name == 'evaljs') { doeval = true; }
}
a.push(o.name + '=' + encodeURIComponent(o.value));
}
$.xml(this.method||'POST', this.action||'', a.join('&'), function(h){
if (doeval === true) {
$.eval(h.responseText);
} else {
$(target).html(h.responseText).find("script").each(function(){
try {
$.eval(this.text || this.textContent || this.innerHTML);
} catch(e){}
});
}
if (callback) { callback(h); }
});
});
};
--markc
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/