如何使这两个事件的代码简短

时间:2020-12-20 00:02:01

I have the following code. how can I make it short so it work with click and enter so I dont have to duplicate it.

我有以下代码。我怎样才能让它变短以便它可以点击并输入所以我不必复制它。

$(document).ready(function(){

    $(document).keypress(function(e) {
     if(e.which == 13) {
       $('form#myform').submit();
     }
   });

   $('#mybutton').click(function() {
     $('form#myform').submit();
   });  

});​

3 个解决方案

#1


4  

this would be a shorter one, it takes advantage of the event bubbling:

这将是一个较短的,它利用事件冒泡:

$(document).ready(function(){
   $(this).on('keypress click', function(e) {
       if(e.which == 13 || e.target.id==='mybutton')
           $('form#myform').submit();
   });  
});​

this is how it works: http://jsfiddle.net/e6L3W/

这就是它的工作原理:http://jsfiddle.net/e6L3W/

#2


1  

Try this:

(Although use of $(this) on document ready is not clear or might be its a typo and it supposed to be a input box.)

(尽管在文档就绪时使用$(this)并不清楚,或者可能是它的拼写错误,它应该是一个输入框。)

Hope this helps the cause and also read the link below: :)

希望这有助于事业,并阅读下面的链接::)

Submitting a form on 'Enter' with jQuery?

使用jQuery在'Enter'上提交表单?

code

$(document).ready(function(){

   $('#search').keypress(function(event) { //<<== #search is input box id
     if ( event.which == 13 ) {
        $('#mybutton').trigger('click');
     }
   })

   $('#mybutton').click(function() {
     $('form#myform').submit();
   });  

});​

OR

      var myFunction = function() {
        $('form#myform').submit();
      }

     $('#search').keypress(function(event) { //<<== #search is input box id
         if ( event.which == 13 ) {
            myFunction;
         }
       })

       $('#mybutton').click(myFunction);  

OR

You could try chaining like this:

你可以尝试像这样链接:

This will bind #element to the events but might be you are looking to bind 2 separate elements with 2 separate event but same outcome.

这会将#element绑定到事件,但可能是您希望将2个单独的元素绑定到2个单独的事件但结果相同。

$('#element').bind('keypress click', function(e) {
    ...
});

#3


1  

$(document).ready(function(){
   $(this).bind('keypress click', function(e) {
       if(e.type == 'click') {
           if(e.target.id == 'mybutton') {
             $('form#myform').submit();
           }
       } else if(e.type == 'keypress') {
           if(e.which == 13) {
             $('form#myform').submit();
           }
       }
   });
});​

#1


4  

this would be a shorter one, it takes advantage of the event bubbling:

这将是一个较短的,它利用事件冒泡:

$(document).ready(function(){
   $(this).on('keypress click', function(e) {
       if(e.which == 13 || e.target.id==='mybutton')
           $('form#myform').submit();
   });  
});​

this is how it works: http://jsfiddle.net/e6L3W/

这就是它的工作原理:http://jsfiddle.net/e6L3W/

#2


1  

Try this:

(Although use of $(this) on document ready is not clear or might be its a typo and it supposed to be a input box.)

(尽管在文档就绪时使用$(this)并不清楚,或者可能是它的拼写错误,它应该是一个输入框。)

Hope this helps the cause and also read the link below: :)

希望这有助于事业,并阅读下面的链接::)

Submitting a form on 'Enter' with jQuery?

使用jQuery在'Enter'上提交表单?

code

$(document).ready(function(){

   $('#search').keypress(function(event) { //<<== #search is input box id
     if ( event.which == 13 ) {
        $('#mybutton').trigger('click');
     }
   })

   $('#mybutton').click(function() {
     $('form#myform').submit();
   });  

});​

OR

      var myFunction = function() {
        $('form#myform').submit();
      }

     $('#search').keypress(function(event) { //<<== #search is input box id
         if ( event.which == 13 ) {
            myFunction;
         }
       })

       $('#mybutton').click(myFunction);  

OR

You could try chaining like this:

你可以尝试像这样链接:

This will bind #element to the events but might be you are looking to bind 2 separate elements with 2 separate event but same outcome.

这会将#element绑定到事件,但可能是您希望将2个单独的元素绑定到2个单独的事件但结果相同。

$('#element').bind('keypress click', function(e) {
    ...
});

#3


1  

$(document).ready(function(){
   $(this).bind('keypress click', function(e) {
       if(e.type == 'click') {
           if(e.target.id == 'mybutton') {
             $('form#myform').submit();
           }
       } else if(e.type == 'keypress') {
           if(e.which == 13) {
             $('form#myform').submit();
           }
       }
   });
});​