如何获得反应中形式的值
我试图以React中形式获取所有输入元素的值。 这是我
export default function MarkAttendance() {
const dispatch = useDispatch();
const navigate = useNavigate();
const { users, loading, error } = useSelector((state) => state.userList);
const today = new Date().toISOString().substr(0, 10);
const { userInfo } = useSelector((state) => state.userLogin);
const [firstTime, setFirstTime] = useState('')
const [secondTime, setSecondTime] = useState('')
const [attenandance, setAttendance] = useState({ id: null, date: today, first_time: "Present", second_time: "Present" });
useEffect(() => {
if (userInfo && userInfo.isAdmin) {
dispatch(listUsers());
} else {
navigate("/login");
}
}, [dispatch, userInfo, navigate]);
const handleSubmit = (e) => {
e.preventDefault();
for (let i = 0; i < users.length; i++) {
setAttendance({ id: users[i].id, date: today, first_time: firstTime, second_time: secondTime });
postAttendance(attenandance);
}
}
return (
<section>
<div className="row">
<div className="col-md-12 text-dark">
<h3>Mark Attendance</h3> for <h4>{today}</h4>'s meal
</div>
</div>
{loading ? (
<Loader />
) : error ? (
<Message variant="danger">{error}</Message>
) : (
<form onSubmit={handleSubmit}>
<table className="table table-bordered">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Room no.</th>
<th scope="col">First Time</th>
<th scope="col">Second Time</th>
</tr>
</thead>
<tbody>
{users.map((user) => (
<tr key={user.id}>
<th scope="row" name="userId">{user.id}</th>
<td className="form-group">
<input
type="text"
className="form-control"
id="table-name"
name="username"
value={user.username}
/>
</td>
<td className="form-group">
<input
type="text"
className="form-control"
id="table-room"
name="room"
value={user.room}
/>
</td>
<td>
<select name={`firstAttendance${user.id}`} onChange={(e) => (e.target.value)} className="form-control" id="table-first-time">
<option value="present">Present ✓</option>
<option value="absent">Absent X</option>
<option value="double">Double 2</option>
</select>
</td>
<td>
<select name={`secondAttendance${user.id}`} onChange={(e) => (e.target.value)} className="form-control" id="table-second-time">
<option value="present">Present ✓</option>
<option value="absent">Absent X</option>
<option value="double">Double 2</option>
</select>
</td>
</tr>
))}
</tbody>
</table>
<button type="submit" className="btn btn-primary">
Submit
</button>
</form>
)}
</section >
);
}
想要的表单是获取选择值以及用户ID并将其发送回服务器。我不知道如何获取用户ID以及他在表格中选择了什么。它是由地图功能制成的,这意味着名称属性都是相同的。
我尝试的
- 是尝试动态地更改
name
属性,但它仍然给我不确定。 - 我尝试使用
usestate
挂钩,但它只给了我最后一个用户的价值,这也改变了其他用户的价值,因为所有挂钩只有一个挂钩。
因此,如何在ReactJ中以形式获取所有值。谢谢
I am trying to get the values of all the input elements in a form in React.
Here is my form
export default function MarkAttendance() {
const dispatch = useDispatch();
const navigate = useNavigate();
const { users, loading, error } = useSelector((state) => state.userList);
const today = new Date().toISOString().substr(0, 10);
const { userInfo } = useSelector((state) => state.userLogin);
const [firstTime, setFirstTime] = useState('')
const [secondTime, setSecondTime] = useState('')
const [attenandance, setAttendance] = useState({ id: null, date: today, first_time: "Present", second_time: "Present" });
useEffect(() => {
if (userInfo && userInfo.isAdmin) {
dispatch(listUsers());
} else {
navigate("/login");
}
}, [dispatch, userInfo, navigate]);
const handleSubmit = (e) => {
e.preventDefault();
for (let i = 0; i < users.length; i++) {
setAttendance({ id: users[i].id, date: today, first_time: firstTime, second_time: secondTime });
postAttendance(attenandance);
}
}
return (
<section>
<div className="row">
<div className="col-md-12 text-dark">
<h3>Mark Attendance</h3> for <h4>{today}</h4>'s meal
</div>
</div>
{loading ? (
<Loader />
) : error ? (
<Message variant="danger">{error}</Message>
) : (
<form onSubmit={handleSubmit}>
<table className="table table-bordered">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Room no.</th>
<th scope="col">First Time</th>
<th scope="col">Second Time</th>
</tr>
</thead>
<tbody>
{users.map((user) => (
<tr key={user.id}>
<th scope="row" name="userId">{user.id}</th>
<td className="form-group">
<input
type="text"
className="form-control"
id="table-name"
name="username"
value={user.username}
/>
</td>
<td className="form-group">
<input
type="text"
className="form-control"
id="table-room"
name="room"
value={user.room}
/>
</td>
<td>
<select name={`firstAttendance${user.id}`} onChange={(e) => (e.target.value)} className="form-control" id="table-first-time">
<option value="present">Present ✓</option>
<option value="absent">Absent X</option>
<option value="double">Double 2</option>
</select>
</td>
<td>
<select name={`secondAttendance${user.id}`} onChange={(e) => (e.target.value)} className="form-control" id="table-second-time">
<option value="present">Present ✓</option>
<option value="absent">Absent X</option>
<option value="double">Double 2</option>
</select>
</td>
</tr>
))}
</tbody>
</table>
<button type="submit" className="btn btn-primary">
Submit
</button>
</form>
)}
</section >
);
}
What I am want is get the select values along with user ids and send it back to the server . I don't know how to get the user id and what he has selected in the form . It is made by a map function which means name attriutes are all gonna be same .
What I have tried
- I tried to change the
name
attribute dynamically but it still give me undefined. - I tried to use
useState
hook but it only gives me the value of last users and this also changes the value of other users since there is only one hook for all.
So how to get all the values in a form in reactjs. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,一段时间后,我终于使用了技巧来获取所有值。我所做的是通过将每个用户的
用户-ID
单独的标签提供给每个id
标签。 >标签ID
。每个用户都有一个唯一的ID,因此输入和选择标签也是如此。然后提取所需输入的值first_time
和second_time
通过提供user-id
,遍历每个输入,并为所有用户和所有用户获取单独的值通过postAttendance
派遣它们。它的外观是:
形式看起来像这样:
So after some time , I finally use the trick to get all the values. What I did is give each
input
tag , a separateid
by incorporating theuser-id
of each user ininput
tagid
.As each user have a unique id so did the input and selection tag . Then extracting the values of desired inputfirst_time
andsecond_time
by providinguser-id
, looping through each inputs and getting separate values for all the users and dispatching them onpostAttendance
.Here is how it look like:
and the form look like this: