[英]javascript change name of called function
Hi all im very new to javascript so please be gentle. 大家好,我是JavaScript的新手,请保持温柔。 im mixing php with my calls and I have a slight issue. 即时通讯与我的电话混用PHP,我有一个小问题。 I need to alter the function name that is called in an onclick event. 我需要更改在onclick事件中调用的函数名称。
<div class=\"span4\" id=\"pass\">
<button class=\"btn btn-block btn-large btn-warning\" id=\"bypass\" disabled onclick=\"pass(); return false;\">
-
</button>
</div>
above is the div with the std call. 上面是带有std调用的div。 before this point some variables are set from another function call and I need to change the above call to "pinpass2() or pinpass3 etc. 在此之前,需要从另一个函数调用中设置一些变量,我需要将上述调用更改为“ pinpass2()或pinpass3等。
function pincheck(id,type,lastid,pin){
document.getElementById('fade').style.display=\"block\";
document.getElementById('calc').style.display=\"block\";
var staffid = id;
document.getElementById('bypass').onclick = function (){\"pinpass\"+staffid();
return false;
};
}
the above is the function that should do it but i can't seem to get it working. 上面是应该执行的功能,但我似乎无法使其正常工作。 Any help appreciated. 任何帮助表示赞赏。 ps if i include the following into the pincheck function the desired staffid is diaplayed ps,如果我在pincheck函数中包含以下内容,则会显示所需的Staffid
alert(\"staff id\"+staffid);
document.getElementById('bypass').onclick = pinpass2;
That should work just fine. 那应该很好。 pinpass2
is already a function, you can assign it to onclick
like any other object (yes, functions are objects in Javascript). pinpass2
已经是一个函数,您可以像其他任何对象一样将其分配给onclick
(是的,函数是Javascript中的对象)。 So just change the onclick
when you need it. 因此,只需在需要时更改onclick
。
If you can't detect changes to the result of staffid()
, then you should use a switch instead. 如果您无法检测到staffid()
结果的更改,则应改用开关。
document.getElementById('bypass').onclick = function() {
switch(staffid()) {
case 1: pinpass(); break;
case 2: pinpass2(); break;
default: pinpass3(); break;
}
};
Though most of the time you don't have to do this. 尽管大多数时候您不必这样做。 Also, I'm not sure if staffid is supposed to be a function or a variable, but it doesn't change anything. 另外,我不确定Staffid应该是函数还是变量,但不会改变任何内容。
By the way, this way of attaching handlers is quite old. 顺便说一下,这种附加处理程序的方法已经很老了。 There's a more powerful one: 还有一个更强大的功能:
document.getElementById('bypass').addEventListener('click', pinpass2, false);
With that you can attach more than one function. 这样一来,您可以附加多个功能。 To remove one: 删除一个:
document.getElementById('bypass').removeEventListener('click', pinpass2, false);
In javascript functions are first class so you can literally just assign pincheck
to another variable like this. 在javascript函数中是一流的,因此您可以从字面上将pincheck
分配给另一个这样的变量。
var pinpass2 = pincheck;
Now you can still call it like this 现在您仍然可以这样称呼它
pinpass(1,2,3,4);
I'm not 100% from your question, but it looks like you are trying to call a different function based on the staffid variable. 我不是100%来自您的问题,但是您似乎正在尝试根据staffid变量调用其他函数。 IE if it is 2
you want to call pinpass2()
. IE,如果它是2
, pinpass2()
调用pinpass2()
。
If this is a global function you can call window[ 'pinpass' + staffid ]()
and it will call the function you want (if it exists). 如果这是全局函数,则可以调用window[ 'pinpass' + staffid ]()
,它将调用所需的函数(如果存在)。
EXAMPLE: if staffid = 2
then window[ 'pinpass' + staffid ]()
is eqivalent to window[ 'pinpass2' ]()
which is the same as calling pinpass2()
. 示例:如果staffid = 2
则window[ 'pinpass' + staffid ]()
与window[ 'pinpass2' ]()
pinpass2()
,与调用pinpass2()
相同。
This works because all global vars (including functions) are properties of the window
object, and properties can be accessed using a dynamically generated name and square bracket notation. 之所以可行,是因为所有全局变量(包括函数)都是window
对象的属性,并且可以使用动态生成的名称和方括号表示法来访问属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.