我不明白为什么我的值是最后一个值data[i].Country
什么时候不应该这样。
for (var i = data.length - 1; i >= 0; i--) {
var test = data[i].Country
// All good \/
console.log(test);
const option = (
// here is the bug \/
<button className="btn" value={i} onClick={() => this.dataHandle(test)} >{test}</button>
);
}
{test}
之间>{test}</button>
很好。第一个console.log(test)
很好。
仅在() => this.dataHandle(test)
中不起作用。
dataHandle (country) {
console.log(country);
}
知道为什么会有这样的行为吗?
我从互联网上尝试了很多方法,但没有一个主题有效:/
编辑:抱歉,我粘贴代码时犯了一个错误
最佳答案
我猜您想将当前值 = {i} 作为数据句柄中的 a 传递。
for (var i = data.length - 1; i >= 0; i--) {
var test = data[i].Country
// All good \/
console.log(test);
const option = (
// Maybe this \/
<button className="btn" value={i} onClick={(e) => this.dataHandle(e.currentTarget, test)} >{test}</button>
);
}
dataHandle (country) {
country = country.textContent
}
关于javascript - 如何绑定(bind)按钮REACT的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61427896/