Last active
December 10, 2015 02:58
-
-
Save rch850/4371373 to your computer and use it in GitHub Desktop.
A jQuery plug-in to switch the action of forms by a select element.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A jQuery plug-in to switch the action of forms by a select element. | |
* Each forms must have data-api attribute. | |
* | |
* EXAMPLE: | |
* | |
* <select name="actionBase"> | |
* <option value="http://localhost/">localhost</option> | |
* <option value="http://dev.example.com/">dev server</option> | |
* </select> | |
* <form data-api="users/name" method="post">...</form> | |
* <form data-api="users/email" method="post">...</form> | |
* <script> | |
* $("select[name=actionBase]").enableAutoSwitch(); | |
* </script> | |
*/ | |
(function($) { | |
$.fn.enableActionSwitch = function() { | |
return this.each(function() { | |
var $this = $(this); | |
function setActionBase() { | |
$("form").each(function() { | |
$(this).attr("action", $this.val() + $(this).data("api")); | |
}); | |
} | |
$this.on("change", function() { | |
setActionBase(); | |
}); | |
setActionBase(); | |
}); | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment