在文本框中按下退格键时发出警报

时间:2013-12-26 06:30:12

标签: javascript jquery typescript

我有一个包含三个只读列的网格。每当用户进入并尝试通过按退格键进行编辑时,我需要通过发送消息进行提醒。我正在使用这个脚本而且它不起作用?任何人都可以纠正我吗?

  $(document).ready(function () {
      $('#txtCode').bind('keypress', function (e) {
          if (e.which == 8) {
              alert('The column is read-only and is not editable');
          }  
      }         
  }); 

9 个答案:

答案 0 :(得分:1)

代替keypress尝试keyupkeydown .on()方法:

$('#txtCode').on('keyup keydown', function (e) {

您也可以绑定多个这样的事件。

并且$('#txtCode')的关闭还有一件事似乎缺少});

  $(document).ready(function () {
     $('#txtCode').on('keyup keydown', function (e) {
       if (e.which == 8) {
         alert('The column is read-only and is not editable');
       }  
     }); //<----");" this is the closing you misssed this         
  });

See the fiddle in action

答案 1 :(得分:1)

如果这是您正在测试的所有代码,那么您没有正确关闭该函数,在我发布的代码中进行了注释。也可以使用keyup代替keypress

$(document).ready(function () {
    $('#txtCode').bind('keyup', function (e) {
        if (e.which == 8) {
            alert('The column is read-only and is not editable');
        }
    }); /*<-- You weren't closing your function properly*/
});

Fiddle

确实需要添加return false语句以确保不会删除字符。我还采用了preventKeyUsage方法进一步扩展了jQuery。

$(document).ready(function () {
    $.fn.preventKeyUsage = function (key, message) {
        return this.each(function () {
            $(this).on('keydown', function (e) {
                return (e.keyCode === key) ? (function () {
                    alert(message);
                    return false;
                })() : true;
            });
        });
    };

    $('#txtCode').preventKeyUsage(8, 'The column is read-only and is not editable');
});

New Fiddle

答案 2 :(得分:0)

您遗失了});的{​​{1}}。谷歌chrome有按键问题,你可以试试keydown

keypress

要在Chrome中使用删除,箭头,退格键,您必须使用keydown。这些键上的按键仅适用于Firefox和Opera。

DEMO

答案 3 :(得分:0)

试试这个

$(document).ready(function () {
    $('#txtCode').on('keyup', function (e) {
        if (e.which == 8) {
            alert('The column is read-only and is not editable');
        }
    }); 
});

DEMO(使用Firefox&amp; Chrome)

答案 4 :(得分:0)

工作代码是:

Javascript代码:

$(document).ready(function () {
    $('#txtCode').bind('keypress keydown', function (e) {
        if (e.which == 8) {
            alert('The column is read-only and is not editable');
        }
    });
});

答案 5 :(得分:0)

这是更新后的代码

<input type="text" id="txtCode" />

$(document).ready(function () {
    $('#txtCode').bind('keydown', function (e) {
        if (e.which == 8) {
            alert('The column is read-only and is not editable');
            return false;
        }
    });
});

Fiddle Demo

答案 6 :(得分:0)

$('#textbox').keydown(function(event){
    if(event.keyCode == 8){
        alert("Backspace not allowed..");
        return false;
    }
});

http://jsfiddle.net/xF9jL/1/

答案 7 :(得分:0)

您可以通过不使用接受输入的元素或使用disabled属性来解决潜在问题:

<textarea name="example" disabled>Some text</textarea>

如果您要回发到服务器,您应该假设用户已经编辑了该字段,无论您采取什么措施来阻止它。

答案 8 :(得分:-1)

keypress事件不会为所有浏览器中的所有密钥提供密钥代码。更好地使用keyupkeydown事件,该事件为所有浏览器中的所有密钥提供密钥代码

为了理解keydown和keypress之间的区别,理解“character”和“key”之间的区别很有用。 “键”是计算机键盘上的物理按钮,而“字符”是通过按下按钮键入的符号。理论上,keydown和keyup事件表示按下或释放的键,而keypress事件表示键入的字符。在所有浏览器中,该理论的实现并不相同。

相关问题