Javascript 在文本框内按backspace时发出警报
我有一个包含三个只读列的网格。每当用户进入那个里并试图通过按backspace进行编辑时,我都需要通过发出消息来提醒。我正在使用这个脚本,但它不起作用?有人能纠正我吗Javascript 在文本框内按backspace时发出警报,javascript,jquery,typescript,Javascript,Jquery,Typescript,我有一个包含三个只读列的网格。每当用户进入那个里并试图通过按backspace进行编辑时,我都需要通过发出消息来提醒。我正在使用这个脚本,但它不起作用?有人能纠正我吗 $(document).ready(function () { $('#txtCode').bind('keypress', function (e) { if (e.which == 8) { alert('The column is read-only and i
$(document).ready(function () {
$('#txtCode').bind('keypress', function (e) {
if (e.which == 8) {
alert('The column is read-only and is not editable');
}
}
});
用
keypress
代替keypress
试试keypup
或keypdown
试试。on()
方法:
$('#txtCode').on('keyup keydown', function (e) {
您也可以像这样绑定多个事件
还有一件事是,$(“#txtCode”)
的结尾似乎丢失了}代码>
$(文档).ready(函数(){
$('#txtCode')。on('keyup keydown',函数(e){
如果(e.which==8){
警报(“该列为只读且不可编辑”);
}
});//您缺少了)按键的code>。google chrome的按键有问题,你可以试试按键
$(document).ready(function () {
$('#txtCode').bind('keydown', function (e) {
if (e.which == 8) {
alert('The column is read-only and is not editable');
}
});
});
要在Chrome中使用删除、箭头和退格键,必须使用keydown。按这些键只能在Firefox和Opera中使用
如果这是您正在测试的所有代码,那么您没有正确关闭函数,并在我发布的代码中进行了注释。也可以使用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*/
});
试试这个
$(document).ready(function () {
$('#txtCode').on('keyup', function (e) {
if (e.which == 8) {
alert('The column is read-only and is not editable');
}
});
});
(在Firefox和Chrome上工作)工作代码是:
Java脚本代码:
$(document).ready(function () {
$('#txtCode').bind('keypress keydown', function (e) {
if (e.which == 8) {
alert('The column is read-only and is not editable');
}
});
});
这是更新后的代码
<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;
}
});
});
$(文档).ready(函数(){
$('#txtCode').bind('keydown',函数(e){
如果(e.which==8){
警报(“该列为只读且不可编辑”);
返回false;
}
});
});
keypress
事件不会为所有浏览器中的所有键提供keycodes。最好使用keyup
或keydown
事件,为所有浏览器中的所有键提供keycode
为了理解keydown和keypress之间的区别,了解“字符”和“键”之间的区别非常有用。“键”是计算机键盘上的一个物理按钮,“字符”是通过按下按钮键入的符号。理论上,keydown和keyup事件表示按下或释放的键,而keypress事件表示键入的字符。这一理论在所有浏览器中的实现并不相同
$('#textbox').keydown(function(event){
if(event.keyCode == 8){
alert("Backspace not allowed..");
return false;
}
});
您可以通过不使用接受输入的元素或使用disabled属性来解决根本问题:
<textarea name="example" disabled>Some text</textarea>
一些文本
如果您要发回服务器,您应该假设用户已经编辑了该字段,无论您如何阻止它。您缺少)
在按键中,这只是一个简单的语法错误!关于代码的用途,为什么只在退格时显示警报?用户可以尝试通过添加新字符进行编辑,也可以选择现有文本和类型来替换它,等等@nnnnnn:如果可以,我可以在脚本中使用| |后跟键代码列表吗?如果(e.which==8 | | e.which==46)是的,你可以,但是列出用户可能按下的所有键是不切实际的。通过不涉及键盘的方法(如编辑菜单或拖放)进行的更改又如何?您对所讨论的元素不仅仅具有只读属性吗?@nnnnnn:老实说,我没有解决方案。有什么办法可以做到吗?@user3003821在head部分添加jquery库。看,把它摆弄一下working@PrasathK-为什么它不能在Chrome中工作?它会在哪些浏览器中工作,为什么?@nnnn请看这里@Prasath K:我正在检查IE8@user3003821使用keyup
或keydown
事件而不是keypress
您是否在头部引用了jquery库?浏览器控制台中有什么错误?如果控制台显示jQuery或$
未定义,则意味着您必须首先在头部提供jQuery库的引用。@user3003821-id属性应该是唯一的。您有三列和(大概)多行,因此如果所有这些字段都具有相同的id=“txtCode”
和$(“#txtCode”)
可能只会找到第一行,那么这将是无效的html。请尝试将它们更改为class=“txtCode”
,并使用$(.txtCode”)
@Jai:我有参考jquery库。当我按下文本框中的键时没有任何操作。请检查左侧哪个jquery文件在我的机器上工作。在chrome中也不能使用按键
$('#textbox').keydown(function(event){
if(event.keyCode == 8){
alert("Backspace not allowed..");
return false;
}
});
<textarea name="example" disabled>Some text</textarea>