javascript - How to pass custom attributes onClick in React
问题描述
I want to pass custom attribute task
to function. following is the code but it is giving undefined
function handleChange(e)
{
console.log(e.target.task)
}
const listTask = props.tasks.map((task)=>
{
return(
<option key={task._id} task={task}>{task.title}</option>
)
})
return(
<select onChange={handleChange}>
{listTask}
</select>
)
in handleChange
method, I want to access task
attribute is there any way?
解决方案
你可以试试这个
function handleChange(e, task) {
console.log(task)
}
<select onChange={e => handleChange(e, task)}>
<option key={task._id} task={task}>
{task.title}
</option>
</select>
参考:将参数传递给事件处理程序
推荐阅读
- google-sheets - 如何使用 Google Sheet API 知道哪些单元格或单元格范围在 Google 电子表格中受到保护?
- javascript - 使用没有任何插件的javascript从一个拖动到另一个
我有两个表:乘客和航班。
<table> <tr> <th> Passenger Name </th> <th> Passenger Flight </th> </tr> <tr> <td> Passenger 1 </td> <td> FR5412 &l
- android - FirebaseCrashlytics.getInstance() 在多进程应用程序中导致应用程序崩溃
- javascript - 如果单击选项卡,下拉菜单关闭
- javascript - 如何等待promise解决然后在reactjs中渲染
- python - 尽管匹配网络表单数据,但无法使用 requests.post 登录网站
- node.js - Node JS 弹性搜索结果改进
- javascript - POST 请求中缺少授权标头
- python - 包含任何元素的列表的最大可能乘积
- php - 问题 SQLSTATE[HY000]: 一般错误:1215 无法添加外键约束