[英]make form visible on keydown event then focus and clear input field
我有一张表格,其中的单元格接受键盘事件。 我也有一个最初隐藏的表单。 我希望按键事件显示表单并将焦点放在输入字段上,但是当我这样做时,触发该事件的按键值会出现在输入字段中-我怎么不显示此字符? (例如,按“ x”,该表单在文本字段中显示为“ x”-我希望该字段为空白)。
table.thistbl td {
border: 2px solid black;
width: 20px;
height: 20px;
padding: 5px;
}
#formid {
display: none;
position: absolute;
width: 300px;
height: 40px;
left: 20px;
top: 60px;
background-color: #CCC;
}
$(function() {
$('#tablediv').on('keydown', 'td', function(e) {
$('#formid').show();
$('#inputid').focus();
});
});
<div id="tablediv">
<table class="thistbl">
<tr>
<td tabindex="0"></td>
<td tabindex="0"></td>
<td tabindex="0"></td>
</tr>
</table>
</div>
<form id="formid">
<input type="text" tabindex="1" id="inputid" />
</form>
尝试使用keyup
而不是keydown
短暂的延迟如何?
$('#tablediv').on('keydown', 'td', function(e) {
setTimeout(funtion(){
$('#formid').show();
$('#inputid').focus();
}, 20);
});
如果您坚持使用keydown
,请添加e.preventDefault()
以防止该事件的默认行为。 但我认为KiiroSora09的答案是切换到keyup
更好。
$(function() { $('#tablediv').on('keydown', 'td', function(e) { e.preventDefault(); $('#formid').show(); $('#inputid').focus(); }); });
table.thistbl td { border: 2px solid black; width: 20px; height: 20px; padding: 5px; } #formid { display: none; position: absolute; width: 300px; height: 40px; left: 20px; top: 60px; background-color: #CCC; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="tablediv"> <table class="thistbl"> <tr> <td tabindex="0"></td> <td tabindex="0"></td> <td tabindex="0"></td> </tr> </table> </div> <form id="formid"> <input type="text" tabindex="1" id="inputid" /> </form>
尝试包括
$('#inputid').focus().attr('value', '');
您实际上在这里所做的是在集中精力之后只是清空了value
像下面
$(function() {
$('#tablediv').on('keydown', 'td', function(e) {
$('#formid').show();
$('#inputid').focus().attr('value', '');
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.