【发布时间】:2021-10-30 19:12:51
【问题描述】:
我试图在onChange 函数中传递<select> 的选定值。
我试过这个 SO Question,但 this 和 this.value 不起作用。我知道这个问题有点老了......
setShippingMethod(obj) {
console.log('Shipping method ' + JSON.stringify(obj));
this.state.defaultMethod = obj.value;
this.props.setShippingMethod(obj.value);
}
render() {
const shippingMethods = this.state.shippingMethods;
const defaultMethod = this.state.defaultMethod;
return (
<aside id="checkout" className="block col-1">
<h1>Shipping</h1>
<div className="row">
<select id="comboMethod" onChange={setShippingMethod(this)} value={!!defaultMethod ? null : defaultMethod.trim()}>
{shippingMethods.map(method => { return <option key={method.shipmthd.trim()} value={method.shipmthd.trim()}>{method.shipmthd.trim()}</option>} )}
</select>
</div>
</aside>
);
}
我的行console.log('Shipping method ' + JSON.stringify(obj)) 生成错误“未捕获的类型错误:循环对象值”,消息似乎是this 仍然指的是整个(在我的情况下)modal。有没有更新的方法来做到这一点?任何帮助将不胜感激。
【问题讨论】:
标签: javascript reactjs