点击按钮没有响应keydown

时间:2013-01-17 16:03:30

标签: jquery

我有以下代码:

 <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 ,但不清楚它们......我知道这不是一个大问题,但无法解决问题......任何帮助请....谢谢

1 个答案:

答案 0 :(得分:1)

您可以将keydown添加到.on-binding:

$(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");
});