admin管理员组文章数量:1122846
Code:
const formSubmit = (event) => {
event.preventDefault();
var data = new FormData(event.target);
let formObject = Object.fromEntries(data.entries());
console.log(formObject);
}
<form onSubmit={formSubmit}>
<table className="center">
<tr className="center">
<th>SSID:</th>
<th><label><input type="text" name="name"/></label></th>
</tr>
<tr className="center">
<th>PASS:</th>
<th><label><input type="text" name="name"/></label></th>
</tr>
</table>
<input type="submit" value="Połącz"/>
</form>
I can obtain data only from second label ("console.log(formObject);"). How to get two strings from two labels via "Object.fromEntries"?
Code:
const formSubmit = (event) => {
event.preventDefault();
var data = new FormData(event.target);
let formObject = Object.fromEntries(data.entries());
console.log(formObject);
}
<form onSubmit={formSubmit}>
<table className="center">
<tr className="center">
<th>SSID:</th>
<th><label><input type="text" name="name"/></label></th>
</tr>
<tr className="center">
<th>PASS:</th>
<th><label><input type="text" name="name"/></label></th>
</tr>
</table>
<input type="submit" value="Połącz"/>
</form>
I can obtain data only from second label ("console.log(formObject);"). How to get two strings from two labels via "Object.fromEntries"?
Share Improve this question edited Nov 22, 2024 at 8:03 Uwe Keim 40.7k61 gold badges185 silver badges302 bronze badges asked Nov 22, 2024 at 7:55 FassetFasset 491 silver badge4 bronze badges2 Answers
Reset to default 3you have both inputs named name
<form onSubmit={formSubmit}>
...
<th><label><input type="text" name="name"/></label></th>
...
<th><label><input type="text" name="name"/></label></th>
...
</form>
that's why you see only second one, because your object will have only one attribute name
and it would be overwritten.
you should change your code so the names of inputs aren't collide:
const formSubmit = (event) => {
event.preventDefault();
var data = new FormData(event.target);
let formObject = Object.fromEntries(data.entries());
console.log(formObject);
}
<form onSubmit={formSubmit}>
<table className="center">
<tr className="center">
<th>SSID:</th>
<th><label><input type="text" name="name1"/></label></th>
</tr>
<tr className="center">
<th>PASS:</th>
<th><label><input type="text" name="name2"/></label></th>
</tr>
</table>
<input type="submit" value="Połącz"/>
</form>
This happens because the same value for name
attribute is used twice--in SSID and PASS, and by doing this you pass in the same value for both. I'll explain how it works.
<form onSubmit={...}>
<tr className="center">
<th>SSID:</th>
<th><label><input type="text" name="name"/></label></th>
</tr>
<tr className="center">
<th>PASS:</th>
<th><label><input type="text" name="name"/></label></th>
</tr>
</form>
The name
attribute in an HTML form acts as the key in key-value pairs when the form data is serialized. Under the hood, the name attribute is used to define a key entry, so when you do something like this input tag:
<input type="text" name="a_name"/>
under the hood, you are doing this: name['a_name'] = 'value_entered_in_input'
When I do
<input type="text" name="a_name"/>
<input type="text" name="a_name"/>
what I'm basically doing is
name['a_name'] = 'first_input_value'
name['a_name'] = 'second_input_value'
In JavaScript, this overwrites the a_name
property from first_input_value
to second_input_value
.
The solution would be to give them different names. For example, instead of using name="name" for both fields, you should use different names for SSID and password:
<form onSubmit={...}>
<tr className="center">
<th>SSID:</th>
<th><label><input type="text" name="ssid"/></label></th>
</tr>
<tr className="center">
<th>PASS:</th>
<th><label><input type="text" name="pass"/></label></th>
</tr>
This will produce an object looking like the below:
{ ssid: 'ssid_value_entered', pass: 'password_value_entered' }
I hope this helped you understand how it works.
本文标签: htmlMultiple labelsone submitStack Overflow
版权声明:本文标题:html - Multiple labels, one submit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736305274a1932533.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论