单击函数-ReactJS时发送行/单元格数据
我正在从componentWillMount检索表数据并将其显示为UI。我想在单击行/单元格时触发一个函数,并将该值返回给另一个PHP,以便使用检索到的单元格值查询后端单击函数-ReactJS时发送行/单元格数据,reactjs,react-jsx,Reactjs,React Jsx,我正在从componentWillMount检索表数据并将其显示为UI。我想在单击行/单元格时触发一个函数,并将该值返回给另一个PHP,以便使用检索到的单元格值查询后端 索引=0,1,2,3(我有4列) 行=每个索引的数据 我不确定为什么this.state.testTime没有向handleClick函数发送正确的值。有人对此有任何意见吗?谢谢 _handleClick: function (event) { this.setState({testTime: event.
- 索引=0,1,2,3(我有4列)
- 行=每个索引的数据
_handleClick: function (event) {
this.setState({testTime: event.target.value});
console.log(event.target.value);
var data = {
testTime: this.state.testTime
}
console.log("clicked");
$.ajax({
type: "GET",
crossDomain: true,
url: "http://localhost:8080/TEST/capture.php",
data: data,
success: function(data){
alert(data);
},
error:function(data)
{
alert("Data sending failed");
}
});
},
return (
<tbody>
{tableData.map((row, index) => {
return (
<tr key={"row_" + index} >
{row.map((cell, index) => {
return (
<td key={"cell_" + index} onClick={this._handleClick} value={this.state.testTime} >{cell}</td>
);
})}
</tr>
);
})}
</tbody>
)
\u handleClick:函数(事件){
this.setState({testTime:event.target.value});
日志(event.target.value);
风险值数据={
testTime:this.state.testTime
}
控制台日志(“单击”);
$.ajax({
键入:“获取”,
跨域:是的,
url:“http://localhost:8080/TEST/capture.php",
数据:数据,
成功:功能(数据){
警报(数据);
},
错误:函数(数据)
{
警报(“数据发送失败”);
}
});
},
返回(
{tableData.map((行,索引)=>{
返回(
{row.map((单元格,索引)=>{
返回(
{cell}
);
})}
);
})}
)
是一个异步函数,它接受回调作为第二个参数,该参数将在setState
完成并重新呈现组件后执行。因此,您需要直接为数据
变量使用event.target.value
,或者将代码放入回调:
this.setState({testTime: event.target.value} ,() => {
var data = {testTime: this.state.testTime}
...
$.ajax...
});