[英]How to get ID of a member from a dropdown box
I am trying to get the best way to get a member's ID so I can send it with an api request, currently I have a drop down menu which shows all the current members.我正在尝试获得获取会员 ID 的最佳方式,以便我可以通过 api 请求发送它,目前我有一个显示所有当前会员的下拉菜单。
<select name="member_id" id="member_id" className={`form-control ${memberSelected ? "is-valid" : "is-invalid"} `} label="Member" placeholder="select" onChange={onChangeMember} required>
<option selected disabled value="">Please select</option>
{members[0].id && members.map((member) => {
return ( <option key={member.id}> {member.id} {member.firstName} {member.surname} </option>
);
})};
</select>
The only way I have so far found to be able to get the ID so I can send it to the api, is to put the member id on the dropdown, get its value and then use a split method to just get the id,到目前为止,我发现能够获取 ID 以便将其发送到 api 的唯一方法是将成员 ID 放在下拉列表中,获取其值,然后使用拆分方法来获取 ID,
membersId.split(" ")[0]
This works fine, but I dont really want to have the id number showing on the dropdown. membersId.split(" ")[0]
这很好用,但我真的不想在下拉列表中显示 ID 号。
So I want to just show the name Joe bloggs
and if they are selected on the dropdown, then this updates membersId
with Joe Bloggs
ID number.所以我只想显示名称Joe bloggs
,如果在下拉列表中选择了它们,那么这会使用Joe Bloggs
ID 号更新membersId
。
<option key={member.id} value={member.id}> {member.firstName, + ' ' + member.surname} </option>
Then you can use onChange event or document.getElementById("idName") for catching the selected member id's然后您可以使用onChange事件或document.getElementById("idName")来捕获选定的成员 ID
Try this..尝试这个..
const handleOnChangeMember = (event) => {
const memberId = event.target.value;
// Do api call with member id
}
<select ... onChange={handleOnChangeMember} required>
<option selected disabled value="">Please select</option>
{
members && members.map((member) =>
<option value={member.id}>{member.firstName} {member.surname} </option>
}
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.