Javascript删除jQuery事件并获取用户输入
我尝试使用的javascript代码有问题。我试图调用一个函数来设置一个事件处理程序,并且在调用事件时,在该事件处理程序中也会删除它。在这个特定的实例中,每当需要输入时,我都会尝试添加一个事件处理程序,然后将回调传递给函数,并在输入就绪时运行代码。在这个阶段,我尝试删除它,这样回调就不会被触发多次,但这似乎有问题。代码如下:Javascript删除jQuery事件并获取用户输入,javascript,jquery,callback,jquery-callback,Javascript,Jquery,Callback,Jquery Callback,我尝试使用的javascript代码有问题。我试图调用一个函数来设置一个事件处理程序,并且在调用事件时,在该事件处理程序中也会删除它。在这个特定的实例中,每当需要输入时,我都会尝试添加一个事件处理程序,然后将回调传递给函数,并在输入就绪时运行代码。在这个阶段,我尝试删除它,这样回调就不会被触发多次,但这似乎有问题。代码如下: this.validateInput = function(NaN, callback) { // Wait for the user to click
this.validateInput = function(NaN, callback) {
// Wait for the user to click submit or press
// enter, then if the input is a valid number
// return true. If it is not valid return false
$(document).keydown(function(e) {
// If the enter key is pressed, if the box is focused and if
if (e.which == 13 && $("#inputBox").is(":focus")) {
// Print the user's input regardless of whether it is a
// number or not.
var newOutput = $("#inputBox").val()
$("#output").append(newOutput + "<br>");
// If the user wants the input to be a number then
// the program checks if the input is not numerical.
if (NaN && !isNaN($("#inputBox").val())) {
// Get input from screen
var newInput = $("#inputBox").val();
// Remove this handler
this.removeKeyhandler();
// Call the code passed to the function
callback(newInput);
// Return from the function.
return;
// This checks if the user wants non-number input
// and runs the following code IF the input is not numerical
} else if (!NaN && isNaN($("#inputBox").val())) {
// Get input from screen
var newInput = $("#inputBox").val();
// Remove this handler
this.removeKeyhandler();
// Call the code passed to the function
callback(newInput);
// Return from the function
return;
}
}
});
}
this.validateInput=函数(NaN,回调){
//等待用户单击提交或按
//输入,然后如果输入是有效数字
//返回true。如果无效,则返回false
$(文档).keydown(函数(e){
//如果按下回车键,框是否对焦,以及
如果(e.which==13&&$(“#输入框”)是(“:焦点”)){
//打印用户的输入,无论其是否为
//数字与否。
var newOutput=$(“#inputBox”).val()
$(“#输出”)。追加(newOutput+”
”;
//如果用户希望输入为数字,则
//程序检查输入是否不是数字。
if(NaN&!isNaN($(“#inputBox”).val()){
//从屏幕获取输入
var newInput=$(“#inputBox”).val();
//删除此处理程序
此参数为.removeKeyhandler();
//调用传递给函数的代码
回调(newInput);
//从函数返回。
返回;
//这将检查用户是否需要非数字输入
//如果输入不是数字,则运行以下代码
}else如果(!NaN&&isNaN($(“#inputBox”).val()){
//从屏幕获取输入
var newInput=$(“#inputBox”).val();
//删除此处理程序
此参数为.removeKeyhandler();
//调用传递给函数的代码
回调(newInput);
//从函数返回
返回;
}
}
});
}
作为参考,#inputBox
是一个输入框,#output
是我试图输出到的
,而removeKeyHandler()
仅包含代码$(document).off(“keydown”,document)代码>。如果要查看完整的文件/项目,请单击
唯一似乎不起作用的是事件处理程序没有删除,它会在添加输入时继续运行很多次。如果你下载并打开,你应该明白我的意思。我明白你的问题。。您在代码中引用的“this”不在正确的范围内
只需这样做:
function display() {
var ref = this;
}
现在替换这些:
this.removeKeyhandler();
为此:
ref.removeKeyhandler();
另外,在删除功能中将其更改为:
$(document).off("keydown");
祝你好运 如果希望keydown事件只触发一次,为什么不使用$(document).one('keydown',function(){….})?@webkit问题是,每当您想按某个键时,它就会触发,但我只希望在只按enter键时将其删除。在这种情况下是。。你所做的是对的,但我在下面的答案中抓住了你的问题。它成功了!谢谢我还是有点不确定为什么我的代码错了,你能解释一下吗?不允许在另一个对象函数内调用其他对象函数吗?关于“范围”有很多信息。。google up'javascript scope'//基本上,javascript中的'this'关键字指的是当前上下文中的对象,在您的例子中是#document。。我建议您研究这个问题,因为它是高级javascript的关键知识;)啊,我明白你的意思了。我完全忘记了事件处理程序的作用域:)