函数 onChange
自动接受唯一的第一个参数(ant design 框架传递这个自动参数)。
函数调用: this.onChange
函数声明:
onChange = (automatic_argument, my_argument) => {
//my code
}
my_argument
可以是我根据调用上下文定义的任一常量。
const LivingRoomOptions = ['a', 'b', 'c'];
const DinningRoomOptions = ['d', 'e', 'f'];
const BedroomOptions = ['g', 'h', 'i'];
我想将这些常量作为第二个参数传递给 onChange
函数,具体取决于调用它的位置。
当我像这样调用函数 this.onChange(my_argument)
时,它只是替换了 automatic_argument
,这是显而易见的。
调用 this.onChange(undefined, my_argument)
,简单地注入(inject) undefined 代替 automatic_argument
。
如何在不干扰 automatic_argument
的情况下将 my_argument
传递给 onChange
?
请引用此链接中的checkall
部分代码。请展开代码。
最佳答案
基本上,要将多个参数传递给处理函数,您应该像下面那样做。
onChange={event => this.onChange(event, my_argument)}
要获取元素的值或 id,请使用 event.target
之类的
onChange = (event, my_argument) => {
console.log("value", event.target.value);
console.log("id", event.target.id);
//if it is checkbox then event.target.checked
console.log("checked", event.target.checked);
}
关于javascript - 将第二个参数传递给自动采用第一个参数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52646199/