我有一个文本框,带有加号和减号符号,可以相应地添加新的文本框并删除已点击的文本框。 我想在使用reactjs的加号上添加onclick事件。 我可以隐藏所选的文本框(对于此函数是:removeChoice),但无法添加文本框(对于该函数是:addChoice)。
var MySelect = React.createClass({
getInitialState: function() {
return {
value: '',
label: ''
}
},
change: function(event) {
this.setState({value: event.target.value});
},
textChange: function(event) {
this.setState({label: event.target.value});
},
addChoice: function(event) {
var a = event.currentTarget.parentNode.parentNode;
$(a).append(); //HERE
},
removeChoice: function(event) {
var a = event.currentTarget.parentNode;
$(a).css('display','none');
},
render: function(){
if($('.selected').length >0 && $('.selected').find('th').length>0 && this.state.label!=''){ $('.selected').find('th')[0].innerHTML = this.state.label; }
if($('.selected').length >0 && $('.selected').find('td').length>0 && this.state.value!='') {
if(this.state.value == "textarea") $('.selected').find('td')[0].innerHTML = '<textarea rows="4" cols="20"></textarea>';
else if(this.state.value == "select") $('.selected').find('td')[0].innerHTML = "<select style='width: 40%'><option></option</select>";
else $('.selected').find('td')[0].innerHTML = '<input type="'+this.state.value+'"/>';
if(this.state.value != "select") $('#selectOption').hide();
}
return(
<div>
<p>Field label</p>
<textarea rows="3" cols="30" className="inputControl" id="field_name" onChange={this.textChange}></textarea><br />
<br />
<p>Field type</p>
<select className="inputControl" id="field_control" onChange={this.change} value={this.state.value}>
<option value="text">Textbox</option>
<option value="number">Number</option>
<option value="checkbox">Checkbox</option>
<option value="radio">Radio</option>
<option value="textarea">Textarea</option>
<option value="select">Dropdown</option>
<option value="date">Date</option>
<option value="email">Email</option>
</select>
<br />
<br />
<div id="selectOption" style={{display: 'none'}}>
<p>
Choices
</p>
<div className="space">
<input type="text" className="inputControl" /></div>
<div className="space">
<input type="text" className="inputControl" />
<i className="fa fa-plus-circle icon add" title="Add another choice" onClick={this.addChoice} ></i><i className="fa fa-minus-circle icon remove" onClick={this.removeChoice}
title="Delete this choice"></i>
</div>
<div className="space">
<input type="text" className="inputControl" />
<i className="fa fa-plus-circle icon add" title="Add another choice" onClick={this.addChoice} ></i><i className="fa fa-minus-circle icon remove" onClick={this.removeChoice}
title="Delete this choice"></i>
</div>
</div>
</div>
);
}
});
有人能建议如何动态添加文本框吗?
render: function() { this.state.textboxes.map(function(answer, i) { return ( <div className="space"> <input key={i} type="text" className="inputControl"/> </div> ); }); }
但是它显示了必须返回一个有效的React组件。您可能已经返回了未定义、数组或其他无效的对象。
错误,你有什么想法吗? - Dhararemove()
函数删除选定的文本框吗? - Dhara