I am counting words in a text field and after a certain amount of words, I use prevent default. In the else, I would like to renable the default commands.
我在文本字段中计算单词,在一定数量的单词后,我使用防止默认。在else中,我想重新设置默认命令。
Does preventDefault() have an opposite function?
preventDefault()是否具有相反的功能?
Here is some sample code:
以下是一些示例代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>preventDefault</title>
<style type="text/css"></style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var wordNum = 3;
var input_length;
$("#max").text("max word(s): " + wordNum);
$("#test").keypress(function(event) {
input_length = $.trim($(this).val())
.replace(/\s+/g, " ")
.split(' ').length;
if (input_length > (wordNum - 1)) {
event.preventDefault();
} else {
return true;
}
});
});
</script>
</head>
<body>
<div id="max"></div>
<textarea id="test" cols="20" rows="5"></textarea>
</body>
</html>
It seems to work on IE, but Firefox doesn't like it.
它似乎适用于IE,但Firefox并不喜欢它。
5 个解决方案
#1
9
I think the problem you are having is that you are not looking for the delete key. Preventdefault does not cancel the event handler all together. But with your code once you hit the maximum length of your field the user will no longer be able delete any characters because the delete keypress is being cancelled.
我认为你遇到的问题是你没有找到删除键。 Preventdefault不会一起取消事件处理程序。但是,一旦您达到字段的最大长度,您的代码将无法再删除任何字符,因为删除了按键。
The reason it works in IE is because IE does not fire the keypress event for delete, end, enter, escape, function keys, home, insert, pageUp/Down and tab. In Safari the keycode for delete is incorrect in the keypress event.
它在IE中工作的原因是因为IE不会为delete,end,enter,escape,function键,home,insert,pageUp / Down和tab激活keypress事件。在Safari中,按键事件中删除的键码不正确。
For these reasons I have a twofold suggestion; first, use the keydown event instead so that you get the correct keycodes.
由于这些原因,我有一个双重的建议;首先,使用keydown事件,以便获得正确的密钥代码。
Second, look at the keycode and if it is delete or backspace then don't preventDefault.
其次,查看密钥代码,如果是删除或退格,则不要阻止默认。
if ((event.keyCode != 46 && event.keyCode != 8) || input_length > (wordNum - 1)) {
return false;
} else {
return true;
}
#2
8
Shouldn't
不能
if maxwords
preventDefault
else
return true
do the trick ?
这个伎俩?
#3
4
simply use return true;
at the end of your eventHandler. That would bubble up the event to the browser.
只需使用return true;在您的eventHandler结束时。这会将事件冒泡到浏览器中。
#4
3
Doesn't look like it, https://developer.mozilla.org/en/DOM/event , but you can just not call it...
看起来不像,https://developer.mozilla.org/en/DOM/event,但你可以不称它为...
#5
2
$("#delete_button").click(function(e){
var category = $("#category").val();
var answer = confirm("Are you sure you would like to delete the " + category + " category?");
if (!answer)
e.preventDefault();
});
#1
9
I think the problem you are having is that you are not looking for the delete key. Preventdefault does not cancel the event handler all together. But with your code once you hit the maximum length of your field the user will no longer be able delete any characters because the delete keypress is being cancelled.
我认为你遇到的问题是你没有找到删除键。 Preventdefault不会一起取消事件处理程序。但是,一旦您达到字段的最大长度,您的代码将无法再删除任何字符,因为删除了按键。
The reason it works in IE is because IE does not fire the keypress event for delete, end, enter, escape, function keys, home, insert, pageUp/Down and tab. In Safari the keycode for delete is incorrect in the keypress event.
它在IE中工作的原因是因为IE不会为delete,end,enter,escape,function键,home,insert,pageUp / Down和tab激活keypress事件。在Safari中,按键事件中删除的键码不正确。
For these reasons I have a twofold suggestion; first, use the keydown event instead so that you get the correct keycodes.
由于这些原因,我有一个双重的建议;首先,使用keydown事件,以便获得正确的密钥代码。
Second, look at the keycode and if it is delete or backspace then don't preventDefault.
其次,查看密钥代码,如果是删除或退格,则不要阻止默认。
if ((event.keyCode != 46 && event.keyCode != 8) || input_length > (wordNum - 1)) {
return false;
} else {
return true;
}
#2
8
Shouldn't
不能
if maxwords
preventDefault
else
return true
do the trick ?
这个伎俩?
#3
4
simply use return true;
at the end of your eventHandler. That would bubble up the event to the browser.
只需使用return true;在您的eventHandler结束时。这会将事件冒泡到浏览器中。
#4
3
Doesn't look like it, https://developer.mozilla.org/en/DOM/event , but you can just not call it...
看起来不像,https://developer.mozilla.org/en/DOM/event,但你可以不称它为...
#5
2
$("#delete_button").click(function(e){
var category = $("#category").val();
var answer = confirm("Are you sure you would like to delete the " + category + " category?");
if (!answer)
e.preventDefault();
});