Javascript 如何访问DOM元素';在React中的事件处理程序函数中的属性和值?

Javascript 如何访问DOM元素';在React中的事件处理程序函数中的属性和值?,javascript,reactjs,dom-events,Javascript,Reactjs,Dom Events,我的渲染函数是 <input type="text" id="search" data-id={form.id} value={form.name} placeholder= "Search..." name="id" onKeyUp={this.keyUpFn}"/> keyUpFn(e){ var textInput = document.getElementById('searc

我的渲染函数是

<input type="text" id="search" data-id={form.id} value={form.name} placeholder= "Search..." name="id" onKeyUp={this.keyUpFn}"/>
keyUpFn(e){
    var textInput = document.getElementById('search');
    value1 = textInput.getAttribute('data-id')
    value2 = e.getAttribute('data-id')
    console.log(value1 )
    console.log(value2 )
}

由于未定义
getAttribute
,这两个控制台值都会给出错误。如何在React中解决此问题?

您可以从事件对象获取数据。这就是代码的外观

keyUpFn(e){
    console.log(e.target.getAttribute('data-id'));
    console.log(e.target.value);
}

实际上,您可以通过访问

event.currentTarget.dataset.paramater;

keyUpFn(e) {
  e.currentTarget.dataset.id
}

您可以使用GetElementsByCassName(或id、名称等)、querySelectorAll、focus。
前两个返回所有有效元素的数组。

您可以在这里找到答案-@tarunluthra这不是我要找的答案。。