jquery - Two or more action buttons on an MVC form - Best practice -


I have an ASP.NET MVC app, which is with a form. Say that this form has an arbitrary button, each of which has its own action.

  & lt; Input type = "button" id = "action1" value = "a" /> & Lt; Input type = "button" id = "action2" value = "two" /> ... and further on  

We want users to click on one and submit a form to fire a jQuery action and then post the form again. with new information.

There are several ways to implement this: What is the best practice about designing this beautifully? If you can, then provide the code snippet.

Set verbatim an object of possible actions:

  var actions = {Action1: function (elem) {// If you just want to submit $ (elem) form normally) .closest ('form');). ...}, Action 2: Function (AMM) {// If you want to load a URL field in a developer URL = $ ('# URL_field') Val (); $ ('Display.') Load (URL) ...}}  

Then type a selector that grabs all the buttons (as you need to be) and attach each function based on the element id on the click handler . Event delegation (using '$ .live ()') You can have multiple buttons, or when buttons are added dynamically, you can increase performance for the case.

  $ ('input [type = button]]'). G ('click', function (e) {action [$ (this) .attr ('id')] (this);});  

It will call related action on each click in the 'verb' variable (you should probably be valid that the function exists).

Then each action function, you can submit it on different URLs, or submit the AJAX, or load new information, or anything you want. As far as 'rewriting of forms' goes, it is a server-side function that prefiles each input element with its previous value, such as you submit invalid.

This is a clean code to make you one line of code to handle events, and then to create each of your actions.


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -