質問

このように見える必要があるIFステートメントがあります:更新

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

入力長が<= 7で、Enterボタンが押されているか、[検索]ボタンがクリックされている場合にのみ、「いくつかの関数」領域を実行しようとしています。

さらに、これら2つの異なる関数イニシエーターを組み合わせて、それらが同じ関数を実行するが、それを行う方法がわからないようにしたい:

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

  AND 

  $("div#search-button").click(function(){
     FUNCTION A
  };
役に立ちましたか?

解決

編集:

これはあなたがしなければならないことです:

  1. テキストボックスの数ではなく、テキストの長さが必要であると仮定しています。
  2. Enterがテキストボックスに押されたときにfunctionaを実行するか、[検索]ボタンがクリックされます。

     $("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

他のヒント

これが私がそれをする方法です:

$("#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!");
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top