/** * jQuery.anchorPost * Converts a standard anchor tag to a postable form * * @author Stephen Rushing * @version 1.0 * @requires jquery.unserialize() * **/ (function(){ var $ = jQuery; $.fn.anchorPost = function(getPostParamsFn){ if(getPostParamsFn==null){ getPostParamsFn = function(){ return $(this).attr("data-post"); } } this.each(function(a, anchor){ var $a = $(anchor), form = $("
").appendTo("body").hide(), formVars = getPostParamsFn.call(anchor); form.attr("target", $a.attr("target")); form.attr("action", $a.attr("href")); if(!$.isPlainObject(formVars)){ formVars = $.unserialize(formVars); } for(var i in formVars){ form.append(""); } //Remove href and target from anchor so it doesn't do anything, which allows it to be used for something else, if the form is being opened in a diff window. $a.attr("href","javascript:;"); $a.removeAttr("target"); $a.click(function(){ form.submit(); }); }); return this; }; });