【发布时间】:2020-12-11 07:14:51
【问题描述】:
我想 console.log 一个已选中复选框的数组。就像选中复选框 1、2 和 5 一样,我希望它在控制台中显示为每个复选框值的数组。我可以获得选择显示在控制台中的最后一个值,但我无法获得所有选中的复选框的数组以显示。它们只是分开显示。
import { CheckboxData } from "../../../Data/SurveyData";
class CheckboxForm extends Component {
constructor(props) {
super(props);
this.state = { value: [] };
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({ value: event.target.value });
console.log("Checkbox: ", event.target.value);
}
handleSubmit(event) {
event.preventDefault();
}
render() {
return (
<div
id="checkboxContainer"
className="container"
onSubmit={this.handleSubmit}
>
{CheckboxData.map((data, key) => {
return (
<div key={key}>
<h5>{data.question}</h5>
<div className="form-check">
<input
className="form-check-input "
required={data.required}
name={data.name}
type={data.type}
value={data.options[0].value}
id={`${data.options[0].name}-${key}`}
onChange={this.handleChange}
/>
<label
className="form-check-label "
htmlFor={`${data.name}-${key}`}
>
{data.options[0].label}
</label>
</div>
<div className="form-check">
<input
className="form-check-input "
required={data.required}
name={data.name}
type={data.type}
value={data.options[1].value}
id={`${data.options[1].name}-${key}`}
onChange={this.handleChange}
/>
<label
className="form-check-label "
htmlFor={`${data.name}-${key}`}
>
{data.options[1].label}
</label>
</div>
<div className="form-check">
<input
className="form-check-input "
required={data.required}
name={data.name}
type={data.type}
value={data.options[2].value}
id={`${data.options[2].name}-${key}`}
onChange={this.handleChange}
/>
<label
className="form-check-label "
htmlFor={`${data.name}-${key}`}
>
{data.options[2].label}
</label>
</div>
<div className="form-check">
<input
className="form-check-input "
required={data.required}
name={data.name}
type={data.type}
value={data.options[3].value}
id={`${data.options[3].name}-${key}`}
onChange={this.handleChange}
/>
<label
className="form-check-label "
htmlFor={`${data.name}-${key}`}
>
{data.options[3].label}
</label>
</div>
<div className="form-check">
<input
className="form-check-input "
required={data.required}
name={data.name}
type={data.type}
value={data.options[4].value}
id={`${data.options[4].name}-${key}`}
onChange={this.handleChange}
/>
<label
className="form-check-label "
htmlFor={`${data.name}-${key}`}
>
{data.options[4].label}
</label>
</div>
</div>
);
})}
</div>
);
}
}
export default CheckboxForm;
SurveyData.js 文件中的数据如下:
{
page: 2,
name: "checkbox",
question: "Check all that apply. I understand:",
type: "checkbox",
options: [
{
name: "checkbox1",
value: "I am entering into a new contract.",
label: "I am entering into a new contract.",
},
{
name: "checkbox2",
value:
"I will be responsible for $49.95 each month until my contract is over.",
label:
"I will be responsible for $49.95 each month until my contract is over.",
},
{
name: "checkbox3",
value: "I have three days to cancel.",
label: "I have three days to cancel.",
},
{
name: "checkbox4",
value:
"If I cancel after three days, I will be responsible for the remainder of the contract.",
label:
"If I cancel after three days, I will be responsible for the remainder of the contract.",
},
{
name: "checkbox5",
value:
"My system is monitored and if it is set off, the cops will come to my home.",
label:
"My system is monitored and if it is set off, the cops will come to my home.",
},
],
},
];```
【问题讨论】:
-
您将字符串分配给复选框值,这很奇怪。
-
复选框表单应该保存所有复选框的状态,而不仅仅是一个复选框。这可能是使用自己的 onChange 处理程序制作 CheckBox 组件的情况,CheckBoxForm 组件使用通过 props 传递的处理程序跟踪复选框数组。
-
@MattDale 我对反应还很陌生,不太明白你的意思。请解释一下好吗?
-
@Cehhiro 我有数值的数字,但我的目标是保存数据并能够调用它,当它是数字时我不知道我在保存什么。
-
@Heather 在构建更复杂的东西之前,您可能首先需要牢牢掌握状态、道具和组件。否则,一旦你处理了这些概念,你可能会在一些简单的事情上不必要地挣扎。
标签: javascript arrays reactjs checkbox