i have a function call it: myFunction(A,B,C)
I have onchange event in my option select that process "myFunction(A,B,C)"
<select id='B' onchange="myFunction(userid,B,C)">
I have two radio buttons that also have an onclick event that process "myFunction(A,B,C)"
<input type=radio, id="Cyes" onclick="myFunction(userid,B,Cyes)">
<input type=radio, id="Cno" onclick="myFunction(userid,B,Cno)">
I am using ajax to process the event dynamically.
my question is, in the onclick event for the radio button, i do not want "myFunction" to process the parameter "B"
and in the onchange event for the options, i do not want "myFunction" to process th开发者_如何学Ce parameter "C"
i am using the same function but i only want the parameters to be processed for that particular onlick/onchange event. i want them to be ignore and i dont want any values pass to the db.
i am not sure if this is doable, but if it is, your input is much appreciated.
thanks
Try something like this by having arguments revert to default values if passing in null or undefined.
function myFunc (A, B, C) {
A = A || "some default value";
B = B || "some default value";
C = C || "some default value";
}
// use A and B, not C:
myFunc('foo', 'bar');
// use A and C, not B:
myFunc('foo', null, 'something');
Alternatively you could pass in an object for the arguments and not worry about order:
function myFunc (argumentsObject) {
var A = argumentsObject.a || "some default value";
var B = argumentsObject.b || "some default value";
var C = argumentsObject.c || "some default value";
}
// use A and B, not C:
myFunc({a: 'foo', b: 'bar'});
// use A and C, not B:
myFunc({a: 'foo', c: 'something'});
ok, so you are actually talking about a javascript function, not a PHP function. Regardless, I would create 2 different functions. Split any common functionality into a third function and call it from the other 2 functions.
Why don't you define additional parameter identificated which event occurred? Or simply define two separate functions? Another way you can check in your code whenever or those parameters are set and decide accordingly, I think there something like is_set(var_name)
in php.
EDIT.
According to you completion of question ywhy don't you simply do this?
function myFunc( A, B, C) {
switch( C) {
case "Cyes" : {
//Do your job here
}
case "Cno" : {
//Do your job here
}
}
}
Sadly JS does not support default arguments (I did not know this), otherwise you could move B to be the last argument and call it like this;
< onclick=MyFunction( A, C, B ) onchange=Myfunction( A, C ) >
But there is a way round it if you really must only have one function:
http://www.openjs.com/articles/optional_function_arguments.php
精彩评论