[英]Delete last added specific symbol, on keydown event?
Well i have a input field and i want to delete dash (-) if someone write it. 好吧,我有一个输入字段,如果有人写,我想删除破折号(-)。 This is my code: 这是我的代码:
$( "#ownerEntrance" ).on( "keydown", function( event ) {
var value = $( "#ownerEntrance" ).val();
if (event.type=="keydown" && event.which == '189') { // 189 for - symbol
$( "#ownerEntrance" ).val(value.substr(0, value.length - 1));
}
});
But now it delete symbol before dash. 但是现在它删除破折号前的符号。 Can i fix it? 我可以解决吗? Thank you! 谢谢!
Try replace
with this regex /(-)/g
on keyup
event: 尝试在keyup
事件中使用此正则表达式/(-)/g
replace
:
$('#no-dash').on('keyup', function() { let $this = $(this); $this.val($this.val().replace(/(-)/g, '')); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="no-dash">
This will remove all dashes even on paste(by keyboard). 这将删除所有破折号,即使粘贴(通过键盘)也是如此。 You can add change
event to remove it in any ways. 您可以添加change
事件以任何方式将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.