返回后的javascript onkeypress调用函数

返回后的javascript onkeypress调用函数,javascript,Javascript,使用Javascript的onKeypress尝试在返回后调用函数。 这不起作用,因为由于验证数字,该字段尚未完成 onKeyPress="return numbersonly(event, false); anotherfunction();" 让这个问题变得棘手的是,返回必须在调用“anotherfunction()”之前发生。使用以下方法: onkeypress="if (numbersonly(event, false)) {anotherfunction();

使用Javascript的onKeypress尝试在返回后调用函数。 这不起作用,因为由于验证数字,该字段尚未完成

onKeyPress="return numbersonly(event, false);
            anotherfunction();"
让这个问题变得棘手的是,返回必须在调用“anotherfunction()”之前发生。

使用以下方法:

onkeypress="if (numbersonly(event, false)) {anotherfunction(); return true;} else {return false}"
由于类似这样的长内联Javascript让我感到厌烦,我将把它移到一个函数中:

function maybe_another(event) {
    if (numbersonly(event, false)) {
      anotherfunction();
      return true;
    } else {
      return false
    }
 }
然后使用:

onkeypress="return maybe_another(event)"
在输入元素中。

使用以下命令:

onkeypress="if (numbersonly(event, false)) {anotherfunction(); return true;} else {return false}"
由于类似这样的长内联Javascript让我感到厌烦,我将把它移到一个函数中:

function maybe_another(event) {
    if (numbersonly(event, false)) {
      anotherfunction();
      return true;
    } else {
      return false
    }
 }
然后使用:

onkeypress="return maybe_another(event)"

在输入元素中。

这将在函数返回后调用另一个函数()

onKeyPress="setTimeout(anotherFunction, 0); return numbersonly(event, false);">
它的行为与巴尔马的回答略有不同。他在调用numbersOnly之后,但在事件处理程序返回之前调用另一个函数。在事件处理程序返回后,Mine将调用另一个函数。两者在不同的情况下都很有用

请注意,如果要将参数传递给其他函数,应执行以下操作:

onKeyPress="setTimeout(function(){anotherFunction(1,2,3);}, 0); return numbersonly(event, false);">

这将在函数返回后调用另一个函数():

onKeyPress="setTimeout(anotherFunction, 0); return numbersonly(event, false);">
它的行为与巴尔马的回答略有不同。他在调用numbersOnly之后,但在事件处理程序返回之前调用另一个函数。在事件处理程序返回后,Mine将调用另一个函数。两者在不同的情况下都很有用

请注意,如果要将参数传递给其他函数,应执行以下操作:

onKeyPress="setTimeout(function(){anotherFunction(1,2,3);}, 0); return numbersonly(event, false);">
尝试以下方法:

function E(e){
  return document.getElementById(e);
}
function FirstFunction(event, truthiness){
  return 'Truthiness was set to '+truthiness;
  //of course you must really be trying to run something from the Event
  //Object or you don't need the event argument in this function or the
  //keyup function below
}
function SecondFunction(e){
  E(e).innerHTML = this.value;
}
E('txt').onkeyup = function(ev){
  var e = ev || window.event;
  E('out1').innerHTML = FirstFunction(ev, false);
  SecondFunction.call(this, 'out2');
}
如果你还不懂
打电话给
这个
,没关系。重点是向您展示,您可以在分配给事件侦听器的函数中放置任意多的函数。这种编程风格可以保存在外部JavaScript文件中,该文件将缓存在用户浏览器中。有些人将其称为JNobsursive JavaScript,因为它不存在于HTML中。另一种方法是,如果出于某种毫无意义的原因,坚持使用突兀的JavaScript,则将第二个函数传递给第一个函数,并在返回值之前执行它,如:

function FirstFunction(func, funcArg, anotherFunc, anotherFuncArg){
  func(funcArg);
  anotherFunc(anotherFuncArg);
}
function SecondFunction(e){
  E(e).innerHTML = 'this is just a test';
}
<input type='text' onkeyup='FirstFunction(SecondFunction, "out", function(){console.log = "You can use an Anonymous Function the same way"})' name='txt' id='txt' />
function FirstFunction(func,funcArg,anotherFunc,anotherFuncArg){
func(funcArg);
anotherFunc(anotherFuncArg);
}
功能第二功能(e){
E(E).innerHTML='这只是一个测试';
}
这将向您展示如何将未执行的函数作为参数传递。稍后使用参数即可。函数名在JavaScript中基本上是可变的。在PHP中,您可以做同样的事情,只需将函数转换为字符串即可

大多数JavaScript程序员都喜欢第一种方法。请参阅下面的小提琴,了解完整的示例

尝试以下方法:

function E(e){
  return document.getElementById(e);
}
function FirstFunction(event, truthiness){
  return 'Truthiness was set to '+truthiness;
  //of course you must really be trying to run something from the Event
  //Object or you don't need the event argument in this function or the
  //keyup function below
}
function SecondFunction(e){
  E(e).innerHTML = this.value;
}
E('txt').onkeyup = function(ev){
  var e = ev || window.event;
  E('out1').innerHTML = FirstFunction(ev, false);
  SecondFunction.call(this, 'out2');
}
如果你还不懂
打电话给
这个
,没关系。重点是向您展示,您可以在分配给事件侦听器的函数中放置任意多的函数。这种编程风格可以保存在外部JavaScript文件中,该文件将缓存在用户浏览器中。有些人将其称为JNobsursive JavaScript,因为它不存在于HTML中。另一种方法是,如果出于某种毫无意义的原因,坚持使用突兀的JavaScript,则将第二个函数传递给第一个函数,并在返回值之前执行它,如:

function FirstFunction(func, funcArg, anotherFunc, anotherFuncArg){
  func(funcArg);
  anotherFunc(anotherFuncArg);
}
function SecondFunction(e){
  E(e).innerHTML = 'this is just a test';
}
<input type='text' onkeyup='FirstFunction(SecondFunction, "out", function(){console.log = "You can use an Anonymous Function the same way"})' name='txt' id='txt' />
function FirstFunction(func,funcArg,anotherFunc,anotherFuncArg){
func(funcArg);
anotherFunc(anotherFuncArg);
}
功能第二功能(e){
E(E).innerHTML='这只是一个测试';
}
这将向您展示如何将未执行的函数作为参数传递。稍后使用参数即可。函数名在JavaScript中基本上是可变的。在PHP中,您可以做同样的事情,只需将函数转换为字符串即可

大多数JavaScript程序员都喜欢第一种方法。请参阅下面的小提琴,了解完整的示例


“尝试在返回后调用函数?”。不可能,这是常见的编程概念。
return
退出函数…是否有jQuery解决方法?内联JavaScript…brrr…“尝试在返回后调用函数?”。不可能,这是常见的编程概念。A
return
退出函数…是否有jQuery解决方法?内联JavaScript…brrr…这是我得到的最接近的结果,但没有解决。onKeypress出现在数量字段中。“anotherfunction()”根据数量重新计算总额。这似乎是一个点击关闭。例如,我点击1,什么都没有发生,然后点击2,它使用1而不是12来计算数量。听起来你应该使用
onkeyup
而不是
onkeypress
。在将新字符添加到输入之前调用keypress处理程序,之后调用keyup处理程序。我添加了一个使用
if
的新解决方案。这与我得到的结果非常接近,但没有引用解决方案。onKeypress出现在数量字段中。“anotherfunction()”根据数量重新计算总额。这似乎是一个点击关闭。例如,我点击1,什么都没有发生,然后点击2,它使用1而不是12来计算数量。听起来你应该使用
onkeyup
而不是
onkeypress
。在向输入中添加新字符之前调用keypress处理程序,之后调用keyup处理程序。我添加了一个新的解决方案,它使用
if
@user2533570 No problem:)很高兴我可以help@user2533570没问题:)很高兴我能帮忙