2

I'm working on a system and I want to make the system easier to use. I have few forms on a page and huge tables in each. I'm not good at JS so any advice would be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Martin
  • 1,193
  • 3
  • 12
  • 24

2 Answers2

7

Use a click event listener:

document.body.addEventListener('click', function(e){
    if (e.button == 1){
       document.formname.submit();
    }
});

EDIT:

As per the new jQuery tag, it's slightly faster:

$('body').click(function(e){
   if((!$.browser.msie && e.button == 1) || ($.browser.msie && e.button == 4)){ 
     // IE exception thanks to @Elias Van Ootegem
     $('form.myForm').submit();
   }
});
Rodik
  • 4,054
  • 26
  • 49
1

Triggering onclick event using middle click

THE Above link will help.

$("#foo").live('click', function(e) { 
   if( e.which == 2 ) {
      e.preventDefault();
      alert("middle button"); 
   }
});

The first line of jQuery allows it to work on the current line page, 'click' its telling it what event it has to listen for, and when the event is called it calls the function defined with the parameter e,

As it is the middle click you are looking for do a if statement to see what has been pressed, in your case you want which to equal 2.

Now as there may be some default actions set for this key, do e.preventDefault() so you able able to use your own code.

Al tough i would recommend using the enter key to submit a form as this is the everyday way of doing it.

I would recommend reading this aswell: http://unixpapa.com/js/mouse.html

Community
  • 1
  • 1
Lemex
  • 3,772
  • 14
  • 53
  • 87
  • -1 for using a library where it doesn't ask for it in the OP and for using [the old](http://stackoverflow.com/questions/5772018/jquery-add-event-handler/5772031#5772031), [*very* slow](http://jsperf.com/jquery-live-vs-delegate-vs-on) and [deprecated](http://api.jquery.com/live/) `.live()` function. – PeeHaa Aug 01 '12 at 08:16
  • Since OP has changed the tags -1 for using the old `.live()` function :-), could you please remove the `.live()` function from your answer and update it with `.on()` so I can revert my downvote? – PeeHaa Aug 01 '12 at 08:18
  • Ok, also updated with JS alternative,explanation and compatibility information within the link – Lemex Aug 01 '12 at 08:19