开发者

how to include multiple if statement conditions in jQuery

I have an if statement that needs to look like this: UPDATE

 $("input#textbox").keypress(function(e){
    key==e.which;
    if($("input#textbox").length <=7 && (key===13 || $("div#search-button").click())){
       /////SOME FUNCTION////
    };
 });

I'm trying to execute the "SOME FUNCTION" area only if the input length is <=7 and either the enter button is pressed or the "search" button is clicked.

Furthermore, I want to combine these 2 different function initiators so that they execute the same function but don't know how to do it:

  $("input#textbox").keypress(function(e){
     FUNCTION A
  };

  AND 

  $("div#search开发者_如何学Go-button").click(function(){
     FUNCTION A
  };


EDIT:

This is what you have to do:

  1. I am assuming that you want the text length and not number of textboxes.
  2. You want to execute FunctionA when enter is pressed on textbox or search button is clicked:

     $("input#textbox").keypress(function(e){
        key==e.which;
        if (key === 13) // if enter is pressed
        {
           if ("#textbox").val().length >= 7) //if textbox has more than 7 characters
           {
               functionA();
           }
        }
     });
     $("div#search-button").click(function(){ functionA();});

HTH


This is how I would do it:

$("#search-button").click(function(){
    $("#textbox").keypress(function(e,clicked){
        (clicked || e.which===13) && $(this).val().length < 8 && functionA();
    }).trigger("keypress",[true]);
});
function functionA(){
    alert("hey!");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜