0

我有以下代码:

 <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并尝试使用列表器,但对它们不清楚....我知道这不是一个大问题,但无法解决问题....请提供任何帮助....谢谢

4

1 回答 1

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");
});
于 2013-01-17T16:08:59.623 回答