简体   繁体   English

如何在我的 class 组件中检测在功能组件中单击了哪个输入值?

[英]How do I detect in my class component which input value's clicked in functional component?

How do I detect in my class component which <input/> 's value was clicked in my functional component?如何在我的 class 组件中检测到在我的功能组件中单击了哪个<input/>value For example, if the user clicks Yes , then I want the postIsClicked() (which's in my class component) function to display that it's indeed detected the Yes that's been clicked.例如,如果用户单击Yes ,那么我希望postIsClicked() (在我的 class 组件中) function 显示它确实检测到了被点击的Yes

My attempt consisted of giving a name to the inputs and then trying to detect is via e.target.name but it just keeps loggin No has been clicked in the console regardless of which button I press (Yes or No)我的尝试包括为输入name ,然后尝试通过e.target.name进行检测,但它只是保持登录 无论我按下哪个按钮(是或否), No has been clicked

What am I doing wrong?我究竟做错了什么?

Pastebin below.粘贴在下面。

https://pastebin.com/tWNM16p3 https://pastebin.com/tWNM16p3

The onClick function can receive the event as argument, so you can access the value by getting e.target.value . onClick function 可以接收事件作为参数,因此您可以通过获取e.target.value来访问该值。

In your case you just need to receive event as argument to postIsClicked(event) and then console.log("clicked" + event.target.value)在您的情况下,您只需要接收event作为postIsClicked(event)的参数,然后console.log("clicked" + event.target.value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将我的 react js 类组件转换为功能组件? - How do I convert my react js class component to a functional component? 我应该使用哪个组件 class 或功能? - Which component should I use class or functional? React - 如何检测是否单击了子组件 - React - How do I detect if a child component is clicked 如何使用 useState - hook 将 class 组件(输入,条件)更改为功能组件? - How to change a class component (input, conditional) to functional component with useState - hook? 我如何定位被点击的 React 组件 - How do i target React component which is clicked 如何反应才能识别它是 class 组件还是功能组件? - How react can Identify it's a class component or functional component? 如何在功能组件或 class 组件之外访问我的 redux state? - How to access my redux state outside of the functional component or class component? 如何将值从子功能组件传递给父类组件? - How to pass value from a child functional component to parent class component? 如何从 class 组件更新功能组件的 state 以便在反应中显示 map 中的项目 - How do I update state of a functional component from a class component in order to display items from a map in react ReactJS class 组件和功能组件如何从普通 Javascript ZC1C425268E68385D1AB50741? - How do I call ReactJS class component and functional component from normal Javascript function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM