[英]How can I use onClick or onSearch event handlers with <select> and <option> elements in ReactJs?
I've found that onClick with option elements works only with Firefox desktop version.我发现带有选项元素的 onClick 仅适用于 Firefox 桌面版本。 It does not work on mobiles and with Chrome desktop/mobile.
它不适用于手机和 Chrome 桌面/移动设备。 I've read that onChange should be used with the select element instead of onClick.
我读过 onChange 应该与 select 元素一起使用,而不是 onClick。 I tried but does not work.
我试过但不起作用。 The original version is something like this:
原始版本是这样的:
const ASV = "ASV";
const ASVtext = "ASVtext";
const KJV = "KJV";
const KJVtext = "KJVtext";
const setBibleVersion = (x, y) => alert(x + " " + y);
export default function App() {
return (
<div className="App">
<h1>onClick / onSearch with select </h1>
<select defaultValue="ASV">
<option value="ASV" onClick={() => setBibleVersion(ASV, ASVtext)}>
{ASVtext}
</option>
<option value="KJV" onClick={() => setBibleVersion(KJV, KJVtext)}>
{KJVtext}
</option>
</select>
</div>
);
} }
And I tried this but this does not work too:我试过这个,但这也不起作用:
const setBibleVersion = x => alert(x);
export default function App() {
return (
<div className="App">
<h1>onClick / onSearch with select </h1>
<select defaultValue="ASV" onSearch={(value) => setBibleVersion(value)}>
<option value="ASV">{ASVtext}</option>
<option value="KJV">{KJVtext}</option>
</select>
</div>
);
}
The main goal is to execute a function with the value of a chosen option.主要目标是使用所选选项的值执行函数。
onSearch
is not an event supported by react yet. onSearch
是 react 支持的事件。 You could instead use onChange
and perform the computation based on the new value that this function provides.您可以改为使用
onChange
并根据此函数提供的新值执行计算。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.