2013-01-17 108 views
0

我有以下代碼:上點擊按鈕不響應的keydown

<ul> 
    <li id="1">same html structure as below...</li> 
    ..... 
    <li id="42"> 
    <div class="input-prepend"> 
     <button class="btn btn-small companyListBtn addMarkerBtn btn-primary btn-warning" type="button"> </button>                     
     <input class="input-medium companyListInput" maxlength="60" type="text"/> 
     <button class="companyListBtn editCompanyBtn" type="button"></button> 
    </div> 
    <div id="42Div" class="companyAddressDiv"> 
     <input type="text" id="test" class="companyAddress" placeholder="Enter the Address"/> 
     <button class="btn btn-small compAddressSubmit" style="height:30px;margin-top:-9px;" type="button"></button> 
    </div> 
    </li> 
    </ul> 



$(document).on('click','.compAddressSubmit', function() { 

    alert("clicked"); 
    //my logic going on here...couldnt mention everything... 

    }); 

所有上面的代碼工作正常....唯一的問題是的onclick當我使用鼠標單擊只執行按鈕......但鍵盤不工作回車鍵...我嘗試 .keydown.bind.live並試圖用listiners但wasnt明確與他們....我知道它不是一個大問題,但不能得到噸他的頭周圍....任何幫助,請....謝謝

+1

你在哪裏試過'.keydown()','.bind()'或'.live()'?我只看到'click'。 – Dom

+0

我的意思是$(document).keydown,$(document).bind ...我不能使用它們的原因是他們給我的邏輯,我實現了非理性... – troy

回答

1

您可以在KEYDOWN添加到。對結合:

$(document).on('click','.compAddressSubmit', function() { 
    alert("clicked"); 
    }).on('keydown','.compAddressSubmit', function(e) { 
     // bind this on the button or directly into the <input>-selector 
     if(e.which === 13) { 
     alert("you pressed enter"); 
     } 
    }); 

而是一種更好的解決辦法是,如果你想觸發表單提交:

$(document).on('submit','#yourformId',function() { 
    alert("your form submit"); 
}); 
+0

我推薦使用'e.which == = 13'。 – Dom

+0

@Dom謝謝,我剛剛進行了更新。 – wildhaber

+0

我以前試過,但由於某種原因沒有工作... – troy