防止非回车键触发提交功能
我正在使用反应组件系统(mantine)。我有一个选择字段,我想使用任意键选择一个选项,然后仅使用回车键提交。
现在的问题是,一旦出现下拉列表并且我按任意键,它就会调用该函数并发送我设置的空字符串。如果表单中没有提交按钮,onSubmit 将无法工作,将显示设置为没有仍然无法提交。所以,我就得到了这个结果。
import React, { useState, useRef } from "react";
import { useNavigate } from "react-router-dom";
import { Text, Select } from "@mantine/core";
import { Icon } from "@iconify/react";
import axios from "axios";
import { api } from "../helpers/api";
const Home = () => {
const [joblist, setJoblist] = React.useState("");
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);
const history = useNavigate();
const selectForm = useRef(null);
function handleSubmit(e) {
if (e.key === "Enter" && e.preventDefault()) {
selectForm.current.submit();
}
setJoblist("");
console.log(joblist);
setLoading(true);
const uu = {
jobs: joblist,
};
console.log(uu);
console.log(api.jobs);
fetch(api.jobs, {
method: "POST",
headers: {
"Access-Control-Allow-Origin": "http://127.0.0.1:8000/",
"Content-Type": "application/json",
},
body: JSON.stringify(uu),
})
.then((res) => {
console.log(res.data);
setLoading(false);
})
.catch((err) => {
console.log(err);
setLoading(false);
setError(err.message || err);
});
}
return (
<div>
<form ref={selectForm} onKeyDown={handleSubmit}>
<Select
label="Your favorite framework/library"
placeholder="Pick one"
searchable
clearable
nothingFound="No options"
value={joblist}
onChange={setJoblist}
data={[
{ value: "react", label: "React" },
{ value: "ng", label: "Angular" },
{ value: "svelte", label: "Svelte" },
{ value: "vue", label: "Vue" },
]}
style={{
width: "608px",
marginLeft: "294px",
marginTop: "-100px",
}}
/>
</form>
</div>
);
};
export default Home;
I am using a react components system(mantine). I have a select field and I want to select an option with any key and submit using the enter key only.
The problem right now is that once the dropdown appears and I press any key it calls the function and sends the empty string i set. The onSubmit won't work without a submit button present in the form, setting display as none still didn't get it to submit. So, I resulted to this.
import React, { useState, useRef } from "react";
import { useNavigate } from "react-router-dom";
import { Text, Select } from "@mantine/core";
import { Icon } from "@iconify/react";
import axios from "axios";
import { api } from "../helpers/api";
const Home = () => {
const [joblist, setJoblist] = React.useState("");
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);
const history = useNavigate();
const selectForm = useRef(null);
function handleSubmit(e) {
if (e.key === "Enter" && e.preventDefault()) {
selectForm.current.submit();
}
setJoblist("");
console.log(joblist);
setLoading(true);
const uu = {
jobs: joblist,
};
console.log(uu);
console.log(api.jobs);
fetch(api.jobs, {
method: "POST",
headers: {
"Access-Control-Allow-Origin": "http://127.0.0.1:8000/",
"Content-Type": "application/json",
},
body: JSON.stringify(uu),
})
.then((res) => {
console.log(res.data);
setLoading(false);
})
.catch((err) => {
console.log(err);
setLoading(false);
setError(err.message || err);
});
}
return (
<div>
<form ref={selectForm} onKeyDown={handleSubmit}>
<Select
label="Your favorite framework/library"
placeholder="Pick one"
searchable
clearable
nothingFound="No options"
value={joblist}
onChange={setJoblist}
data={[
{ value: "react", label: "React" },
{ value: "ng", label: "Angular" },
{ value: "svelte", label: "Svelte" },
{ value: "vue", label: "Vue" },
]}
style={{
width: "608px",
marginLeft: "294px",
marginTop: "-100px",
}}
/>
</form>
</div>
);
};
export default Home;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当
e.key === "Enter"
为 false 时,条件评估将立即停止。所以尝试一下:
你可能需要这个来阻止执行代码的其余部分(但这还不清楚......)
With
When
e.key === "Enter"
is false, the condition evaluation stops right there.So try:
And you may need this to prevent executing the rest of the code (but that is unclear...)