antd输入框输入的是中文etargetvalue获取的是拼音再次回车才是中文怎么解决
您可以使用onCompositionStart和onCompositionEnd事件来解决这个问题。当用户输入中文时,浏览器会触发onCompositionStart事件,此时您可以暂时忽略e.target.value的值。当用户输入完成,浏览器会触发onCompositionEnd事件,此时您可以通过e.target.value获取到用户输入的中文。
以下是一个示例代码:
import React, { useState } from "react";
const InputComponent = () => {
const [inputValue, setInputValue] = useState("");
const handleInputChange = (e) => {
const value = e.target.value;
setInputValue(value);
};
const handleCompositionStart = () => {
// 忽略拼音输入
};
const handleCompositionEnd = (e) => {
const value = e.target.value;
setInputValue(value);
};
return (
<input
type="text"
value={inputValue}
onChange={handleInputChange}
onCompositionStart={handleCompositionStart}
onCompositionEnd={handleCompositionEnd}
/>
);
};
export default InputComponent;
在这个示例中,当用户输入中文时,拼音会先出现在input框中,但是我们会忽略这些拼音输入。当用户输入完成,再次回车时,浏览器会触发onCompositionEnd事件,此时我们可以获取到用户输入的中文,并更新输入框的值
原文地址: https://www.cveoy.top/t/topic/hH7j 著作权归作者所有。请勿转载和采集!