Syd*_*ria 9 reactjs redux-form
我想问一下,这是一个场景.我有这个多个复选框但我的问题是每当我勾选一个复选框时,所有4个复选框都被选中.而且为什么复选框的值只是真或假.这是我的复选框:
<div className="checkbox">
<label><Field name="investor_stage" component="input" type="checkbox" value="Seed" /> Seed</label>
</div>
<div className="checkbox">
<label><Field name="investor_stage" component="input" type="checkbox" value="Early Stages" /> Early Stages</label>
</div>
<div className="checkbox">
<label><Field name="investor_stage" component="input" type="checkbox" value="Formative Stages" /> Formative Stages</label>
</div>
<div className="checkbox">
<label><Field name="investor_stage" component="input" type="checkbox" value=" Later Stages" /> Later Stages</label>
</div>
Run Code Online (Sandbox Code Playgroud)
Aft*_*eed 15
对于像我这样的人来说,对于redux和反应都是新手,可能会发现这里提到的原始代码令人困惑.我修改并将其转换为ES6类.我还删除了bootstrap,验证并使其易于调试.
这是修改后的代码
import React from 'react';
class CheckboxGroup extends React.Component {
checkboxGroup() {
let {label, required, options, input, meta} = this.props;
return options.map((option, index) => {
return (
<div className="checkbox" key={index}>
<label>
<input type="checkbox"
name={`${input.name}[${index}]`}
value={option.name}
checked={input.value.indexOf(option.name) !== -1}
onChange={(event) => {
const newValue = [...input.value];
if (event.target.checked) {
newValue.push(option.name);
} else {
newValue.splice(newValue.indexOf(option.name), 1);
}
return input.onChange(newValue);
}}/>
{option.name}
</label>
</div>)
});
}
render() {
return (
<div>
{this.checkboxGroup()}
</div>
)
}
}
export default CheckboxGroup;
Run Code Online (Sandbox Code Playgroud)
用法:
let optionsList = [{id: 1, name: 'Optoin1'}, {id: 2, name: 'Option 2'}, {id: 3, name: 'Option 3'}]
<Field name="roles" component={CheckboxGroup} options={optionsList} />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9021 次 |
| 最近记录: |