对于input
案例,我无法删除和/或添加任何字符。我看到单选按钮的值在console.log
中更改,但在视觉上没有。我不确定为什么它会这样表现。
handleChange: function(e) {
switch (e.target.name) {
case 'name':
this.setState({newName: e.target.value}); break;
case 'type':
this.setState({newType: e.target.value}); break;
}
nameFormatter: function(id, name) {
return (this.state.editEnabled && this.state.editId === id) ?
<input type="text"
value={name}
name="name"
maxLength="50"
onChange={this.handleChange} /> : name;
},
typeFormatter: function(id, type) {
type = type === 'blacklist' ? 'Blacklist' : 'Whitelist';
return (this.state.editEnabled && this.state.editId === id) ?
<div>
<div><input type="radio"
name="ipsetType"
value="blacklist"
checked={type === 'Blacklist'}
onChange={this.handleChange} /> Blacklist</div>
<div><input
type="radio"
name="type"
value="whitelist"
checked={type === 'Whitelist'}
onChange={this.handleChange} /> Whitelist</div>
</div> : type;
},
答案 0 :(得分:2)
这是因为您的handleChange
设置了newName
和newType
的状态,而您需要为name and type
handleChange: function(e) {
switch (e.target.name) {
case 'name':
this.setState({name: e.target.value}); break;
case 'type':
this.setState({type: e.target.value}); break;
}
答案 1 :(得分:0)
尝试onchange而不是onChange。
答案 2 :(得分:0)
似乎您正在更改错误的值,还要确保已检查的属性与正确的值进行比较。试试这个:
handleChange: function(e) {
var values;
switch (e.target.name) {
case 'name':
values = {name: e.target.value}; break;
case 'type':
values = {type: e.target.value}; break;
}
// Try to always have a single call to setState in your methods
this.setState(values);
}
nameFormatter: function(id, name) {
return (this.state.editEnabled && this.state.editId === id) ?
<input type="text"
value={name}
name="name"
maxLength="50"
onChange={this.handleChange} /> : name;
},
typeFormatter: function(id, type) {
// don't mutate the params
//type = type === 'blacklist' ? 'Blacklist' : 'Whitelist';
// instead create a new variable
var label = type === 'blacklist' ? 'Blacklist' : 'Whitelist';
return (this.state.editEnabled && this.state.editId === id) ?
<div>
<div><input type="radio"
name="ipsetType"
value="blacklist"
checked={type === 'blacklist'}
onChange={this.handleChange} /> Blacklist</div>
<div><input
type="radio"
name="type"
value="whitelist"
checked={type === 'whitelist'}
onChange={this.handleChange} /> Whitelist</div>
</div> : label ;
},