Enable Backspace with jQuery oninput event within the text on an input form?

巧了我就是萌 提交于 2019-12-13 08:01:34

问题


I have an input form that has a jQuery event applied to it that creates square brackets around the users input. The issue is that the event prevents the ability to use backspace to delete the text. I'm looking for a way to check if backspace is being pressed and prevent the code from runnning.

jQuery Script

function test(obj) {
var oldVal = obj.val().replace(/\[/g,\'\');
oldVal = oldVal.replace(/\]/g,\'\');
var val = \'[\' + oldVal + \']\';
obj.val(val);

Input Form

oninput="test($(this));"

回答1:


 function test(obj,e) {  
      if(!(e.which == 8)){
           var oldVal = obj.val().replace(/\[/g,'');
           oldVal = oldVal.replace(/\]/g,'');
           var val = '[' + oldVal + ']';
           obj.val(val);
      }
 }

and pass event as parameter ..

 <input class="text-box" onkeyup="test($(this),event)">


来源:https://stackoverflow.com/questions/25310701/enable-backspace-with-jquery-oninput-event-within-the-text-on-an-input-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!