[英]addNewRow with Enter Key via javascript
I have the following code : 我有以下代码:
$(document).on('keypress', ".addNewRow", function(e){
var keyCode = e.which ? e.which : e.keyCode;
if(keyCode == 9 ) addNewRow();
});
Upon pressing "Tab" this code executes addNewRow (which adds table data). 按下“ Tab”键后,此代码将执行addNewRow(添加表数据)。 This works perfect.
这完美。 I want to alter this to make it work with "Enter", so I modified as follows :
我想更改它以使其与“ Enter”一起使用,因此我进行了如下修改:
$(document).on('keypress', ".addNewRow", function(e){
var keyCode = e.which ? e.which : e.keyCode;
if(keyCode == 9,13 ) addNewRow();
});
For some odd reason, this does not work as expected. 由于某些奇怪的原因,这不能按预期方式工作。 Is there a different way of doing this that I am missing?
我是否有其他方式无法做到这一点? *Banging head on wall
*在墙上的撞头
Edit : Also have tried this : 编辑:也尝试过这个:
$(document).on('keypress', ".addNewRow", function(e){
var keyCode = e.which ? e.which : e.keyCode;
if(keyCode == 9||13 ) addNewRow();
});
Form Code(To prevent submit on enter) : 表格代码(以防止在输入时提交):
<script type="text/javascript">
function tabE(obj, e) {
var e = (typeof event != 'undefined') ? window.event : e; // IE : Moz
var self = $(obj),
form = self.parents('form:eq(0)'),
focusable, next;
if (e.keyCode == 13) {
focusable = form.find('input,a,select,button,textarea').filter(':visible');
next = focusable.eq(focusable.index(obj) + 1);
if (!next.length) {
next = focusable.first();
}
next.focus();
return false;
}
}
</script>
<!-- Prevent Enter from Submitting Form -->
<script>
$(document).on("keydown", "input", function(e) {
if (e.which==13) e.preventDefault();
});
</script>
<!-- Begin page content -->
正确or
声明:
if (keyCode == 9 || keyCode == 13)
https://jsfiddle.net/8bzn43r6/ Please see the example in the link https://jsfiddle.net/8bzn43r6/请参阅链接中的示例
<table>
<tbody>
<tr>
<td><input type = "text" id="username"/></td>
</tr>
</tbody>
</table>
$(document).ready(function(){
var count = 0;
function addNewRow(){
console.log("Added ROW" + count++);
$("<tr><td><input type='text' id='username'/><td/><tr/>").appendTo("tbody");
}
$('tbody').on('keydown','#username',function(e){
if(e.keyCode == 9 || e.keyCode == 13) addNewRow();
});
});
Working like a charm 像魅力一样工作
do you want your if as an OR? 您是否希望将自己作为OR?
if(keyCode == 9 || keyCode == 13 )
or as an AND? 还是作为AND?
if(keyCode == 9 && keyCode == 13 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.