简体   繁体   English

单击输入键/返回键应触发提交按钮

[英]Clicking the enter key/return key should trigger the submit button

I am trying to add the items using the submit button click event. 我正在尝试使用“提交”按钮单击事件添加项目。 Right now I am trying to implement it so that when you enter the number to the input and hit enter, it should trigger the submit button and add the item to the table. 现在,我正在尝试实现它,以便当您在输入中输入数字并按回车键时,它应触发“提交”按钮并将该项添加到表中。 I used the keypress event and it is not working. 我使用了keypress事件,但无法正常工作。 Here is my sample code . 这是我的示例代码

I think I am doing something wrong. 我想我做错了。 My code below. 我的代码如下。

 // json data object var data = JSON.parse('{ "122233334444": ["Book","Three Musketters","DE7598490587","7584092043857", "03/18/13 11:17:51 AM","03/18/13 11:17:51 AM", "1" ], "122223355552":["eBook","Fall Colors","XYZ29494949","7584092043857", "03/18/13 11:17:51 AM","03/18/13 11:17:51 AM", "2" ], "122223355533":["eBook","Snowfall","XYZ29494949","7584092043857", "03/18/13 11:17:51 AM","03/18/13 11:17:51 AM", "3" ] }'); $("#submitid").keypress(function() { $('#resend').prop('disabled', false); $('#receipt').prop('disabled', false); var rowId = $("#number").val(); $("#number").val(""); var rowData = data[rowId]; if (rowData) { var tr = $("<tr><td><input id='item-id' type='checkbox' checked/></td></tr>").attr("id", "datatable-row-" + rowId); for (var col = 0; col < rowData.length; col++) tr.append($("<td></td>").text(rowData[col])); $("#datatable").prepend(tr); $("#datatable").show(); } else { alert("Row doesn't exist in json object: " + rowId); } }); $('#receipt').click(function() { $('.column-id').removeAttr('checked'); $('#resend').prop('disabled', true); $('#receipt').prop('disabled', true); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="number" type="text" /> <button id="submitid">Submit</button> <table id="datatable"> <colgroup> <col><col><col><col><col><col><col><col> </colgroup> <thead> <tr> <th rowspan="1" colspan="1"> <input type="checkbox" class="select-all column-id" id="item-id" title="Select All"> </th> <th rowspan="1" colspan="1"> <div> <span><a href="#" title="">Format</a></span> </div> </th> <th rowspan="1" colspan="1"> <div> <span><a href="#" title="">Title</a></span> </div> </th> <th rowspan="1" colspan="1"> <div> <span><a href="#" title="">Number</a></span> </div> </th> <th rowspan="1" colspan="1"> <div> <span><a href="#" title="">Code</a></span> </div> </th> <th rowspan="1" colspan="1"> <div> <span><a href="#" title="">Checkout Date</a></span> </div> </th> <th rowspan="1" colspan="1"> <div> <span><a href="#" title="">Due Date</a></span> </div> </th> <th rowspan="1" colspan="1"> <div> <span><a href="#" title="">ItemId</a></span> </div> </th> </tr> </thead> <tbody> <tr> <td><input id="item-id" class="column-id" type="checkbox"></td> <td>Book</td> <td>Three Musketters</td> <td>DE7598490587</td> <td>7584092043857</td> <td>03/18/13 11:17:51 AM</td> <td>03/18/13 11:17:51 AM</td> <td>1</td> </tr> </tbody> </table> <button id="resend" disabled>Resend</button> <button id="receipt" disabled>Receipt</button> 

Any suggestions? 有什么建议么?

krish krish

You need to handle keydown or keypress on the input not on the submit button: 你需要处理keydownkeypress没有提交按钮输入:

$('#number').keydown(function (event) {
    if (event.keyCode === 13) {
        // Enter was pressed
    }
});

Following are the things you should know 以下是您应该知道的事情
1) You need to know to keycode of enter key (which is 13 ) 1)您需要知道Enter键的键码 (即13

2) Since you tagged jQuery, learn event.which , this property indicates the specific key or button that was pressed. 2)由于标记了jQuery,请学习event.which ,此属性指示所按下的特定键或按钮。

3) .trigger() , execute all handlers and behaviors attached to the matched elements for the given event type. 3) .trigger() ,对于给定的事件类型,执行附加到匹配元素的所有处理程序和行为。

$("#number").on('keypress', function (e) {
  if(e.which == 13) {  // check keycode condition       
   $('#submitid').trigger('click');
  }
  else {
  //Todos
  }
  e.preventDefault(); //Toprevent bounce
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM