gpt4 book ai didi

reactjs - React 如何制作一个具有固定值和动态值的输入字段

转载 作者:行者123 更新时间:2023-12-05 04:31:46 25 4
gpt4 key购买 nike

关于我如何能像这个人在 React 中使用 Javascript 那样获得类似结果的任何解决方案?检查链接 -> https://codepen.io/matheusls94/pen/WOdRPR

我有一个输入字段/文本字段(mui),当新用户想要注册一个新帐户/电子邮件地址时,它会向服务器发出发布请求。我希望用户避免输入“@hotmail.com”,它应该是输入字段中不可编辑/不可删除的固定值。就像上面的 codepen 链接一样。

动态值示例 -> Testemail,固定值 -> @hotmail.com

  const [email, setEmail]=useState("");

async function Register(e){
let item = {email};
e.preventDefault();
setLoading(true);

let result = await fetch("https://localhost:44334/api/Register",{
method:"POST",
headers:{
"Accept": "text/plain",
"Content-Type": "application/json-patch+json",
},
body:JSON.stringify(item),
});
result = await result.json();

return (
<>
<Container className="mt-5">
<Row>
<Col lg={4} md={6} sm={12} className="text-center mt-5 p-3">
<img className="icon-img" src={loginIcon} alt="icon"/>
<Form onSubmit={Register}>
<Form.Group>
<Form.Label>E-Mail</Form.Label>
<input value={dynamicvalue+fixedvalue} type="text" required={true} onChange={(e) => setEmail(e.target.value)} fullWidth />
</Form.Group>
</>
)

最佳答案

您可以将 "@hotmail.com" 附加到输入的 value 属性,并在更新状态时将其删除。检查输入值是否以 "@hotmail.com" 结尾,以及它是否允许更新状态。

例子

function App() {
const [email, setEmail] = React.useState("");

const changeHandler = (e) => {
const { value } = e.target;
if (value.endsWith("@hotmail.com")) {
const modValue = value.replace("@hotmail.com", "");
setEmail(modValue);
}
};

return (
<div className="App">
<input
type="text"
value={`${email}@hotmail.com`}
onChange={changeHandler}
/>
</div>
);
}

ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="root" />

更新

How would I achieve this if the values in modValue was to include"@hotmail.com" ? instead of replacing it with an empty string? Forsome reason the input value "@hotmail.com" does not get included onthe registered user, only the dynamic value.

正确。如果在别处使用,您需要将 "@hotmail.com" 追加回 email 值。

但你是对的,有一种方法可以初始化 email 状态 "@hotmail.com" 并做同样的 .endsWith 测试并在没有字符串替换的情况下进行状态更新。

function App() {
const [email, setEmail] = React.useState("@hotmail.com");

const changeHandler = (e) => {
const { value } = e.target;
if (value.endsWith("@hotmail.com")) {
setEmail(value);
}
};

return (
<div className="App">
<input
type="text"
value={email}
onChange={changeHandler}
/>
</div>
);
}

ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="root" />

关于reactjs - React 如何制作一个具有固定值和动态值的输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71793604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com