Commit d7856eb2 authored by Gilles van den Hoven's avatar Gilles van den Hoven

$.fn.formValues;

Gets form values and creates a key=>value array of the found values.

What's new?
- Only does this for ENABLED elements.
- Keeps the same order of the form.
- Optionally adds the button which is clicked (marks that name with an 'x' in the list)

example: $('#frmLogin').formValues('oButton');



$.fn.update (PREVIOUSLY: $.update, so beware!!!!)
Calls sURL with sAction (method) and sends the aValues. Puts the results from that call in the jQuery object and calls fCallback if provided.

What's new?
- Renamed $.update to $.fn.update, since it is more obvious to call $('someJQueryObject').update(...) then $.update($('someJQueryObject'), ...). It's also more jQuery-ish
- Added the method you want to use, since i used post before, now you can select between either GET or POST.

example: $('someJQueryObject').update('sURL', 'sAction', 'aValues', 'fCallback');



$.fn.serialize
Calls the form's action with the correct method and the serialized values. Optionally adds the button which is clicked if you provide it. When there are results, the fCallback function is called.

What's new?
- The entire function

example: $('someForm').serialize('sButton', 'fCallback');
parent 497bfb79
...@@ -133,52 +133,98 @@ $.fn.load = function(a,o,f) { ...@@ -133,52 +133,98 @@ $.fn.load = function(a,o,f) {
}; };
/** /**
* function: $.fn.formValues * name: $.fn.formValues
* usage: $('#frmLogin').formValues() * example: $('#frmLogin').formValues('sButton')
* docs: Gets form values and creates a key=>value array of the found values (for ENABLED elements!) * docs: Gets form values and creates a key=>value array of the found values.
* Optionally adds the button which is clicked if you provide it.
* Only does this for ENABLED elements in the order of the form.
*/ */
$.fn.formValues = function() { $.fn.formValues = function(sButton) {
var a = []; var a = [];
$("input,textarea,option",this).filter(":enabled").each(function(){ var elp = {INPUT:true, TEXTAREA:true, OPTION:true};
// Skip selects with options which are not selected
if ((this.parentNode.type == 'select-one' || this.parentNode.type == 'select-multiple') && !this.selected) {
return null;
}
// Skip radio and checkbox elements which are not checked // Loop the shite
if ((this.type == 'radio' || this.type == 'checkbox') && !this.checked) { $('*', this).each(function() {
return null; // Skip elements not of the types in elp
} if (!elp[this.tagName])
return;
// Skip disabled elements
if (this.disabled)
return;
// Skip non-selected nodes
if ((this.parentNode.nodeName == 'SELECT') && (!this.selected))
return;
// Skip non-checked nodes
if (((this.type == 'radio') || (this.type == 'checkbox')) && (!this.checked))
return;
// All other elements are valid // If we come here, everything is fine, so add the data
a.push({ a.push({
name: this.name || this.id || this.parentNode.name || this.parentNode.id, name: this.name || this.id || this.parentNode.name || this.parentNode.id,
value: this.value value: this.value
}); });
}); });
// Add submit button if needed
if (sButton && (sButton !== null))
a.push({ name: sButton, value: 'x' });
return a; return a;
}; };
/** /**
* function: $.update * name: $.fn.update
* usage: $.update('someJQueryObject', 'someurl', 'array'); * example: $('someJQueryObject').update('sURL', 'sAction', 'aValues', 'fCallback');
* docs: Mimics the ajaxUpdater from prototype. Posts the key=>value array to the url and * docs: Calls sURL with sAction and sends the aValues
* puts the results from that call in the jQuery object specified. * Puts the results from that call in the jQuery object and calls fCallback
* --> If you set the blnNoEval to true, the script tags are NOT evaluated.
*/ */
$.update = function(objElement, strURL, arrValues, fncCallback) { $.fn.update = function(sURL, sMethod, aValues, fCallback) {
$.post(strURL, arrValues, function(strHTML) { var el = this;
// Update the element with the new HTML
objElement.html(strHTML); // Process
$.xml(
// Evaluate the scripts sMethod || "GET",
objElement.html(strHTML).find("script").each(function(){ sURL || "",
try { $.param(aValues),
$.eval( this.text || this.textContent || this.innerHTML ); function(sResult) {
} catch(e) { } sResult = $.httpData(sResult);
});
// Update the element with the new HTML
el.html(sResult);
// Evaluate the scripts AFTER this (so you can allready modify the new HTML!)
el.html(sResult).find("script").each(function(){
try { $.eval( this.text || this.textContent || this.innerHTML ); } catch(e) { }
});
// And call the callback handler :)
if (fCallback && (fCallback.constructor == Function))
fCallback();
}
);
};
// Callback handler /**
if (fncCallback) { fncCallback(); } * name: $.fn.serialize
}); * example: $('someJQueryObject').serialize('sButton', 'fCallback');
* docs: Calls the form's action with the correct method and the serialized values.
* Optionally adds the button which is clicked if you provide it.
* When there are results, the fCallback function is called.
*/
$.fn.serialize = function(sButton, fCallback) {
var el = this.get(0);
// Process
$.xml(
el.method || "GET",
el.action || "",
$.param(this.formValues(sButton)),
function(sResult) {
if (fCallback && (fCallback.constructor == Function))
fCallback($.httpData(sResult));
}
);
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment