gpt4 book ai didi

javascript - 在 React Js 中添加和删除输入字段,且值不会重置

转载 作者:行者123 更新时间:2023-12-03 06:58:12 25 4
gpt4 key购买 nike

嗨,我正在开发一个 React 应用程序,其中有四个选项。当用户选择一个选项时,相应的输入元素将被添加到包装器中。在下面的代码中,添加操作工作正常,但删除操作无法正常工作,没有删除相应的元素。另一个问题是组件重新渲染时输入字段上的值不存在。因此专家指导我如何实现单击删除按钮时删除相应的行并且组件重新渲染时不应重置输入值

import React from 'react';
import ReactDOM from 'react-dom';

var fields = "";
var options = ['one','two','three','four','five','six','seven','eight','nine','Ten','eleven','twelve'];

var SelectOptions = React.createClass({
render:function(){
var options = this.props.options;
return (
<select>
{options.map(function(col,index){
return (
<option key={index} value ={col}> {col} </option>
);
})}
</select>
);

}
});

var PriceMultiply = React.createClass({
render:function(){
return (
<tr>
<td>
Adjust Price Multiply
</td>
<td>
Multiply <SelectOptions options={options} />
</td>
<td>
by <input type="text" name="" />
</td>
<td>
<button className="btn btn-danger" onClick={this.props.removeOperation} > Remove </button>
</td>
</tr>
);
}
});

var PriceAdd = React.createClass({
render:function(){
return (
<tr>
<td>
Adjust Price (Add)
</td>
<td>
Add <input type="text" name="" />
</td>
<td>
to <SelectOptions options={options} />
</td>
<td>
<button className="btn btn-danger" onClick={this.props.removeOperation} > Remove </button>
</td>
</tr>
);
}
});

var ExcludeProducts = React.createClass({
render:function(){
return (
<tr>
<td>
Filter Products (Includes)
</td>
<td>
Exclude Products Where <SelectOptions options={options} />
</td>
<td>
Includes <input type="text" name="" />
</td>
<td>
<button className="btn btn-danger" onClick={this.props.removeOperation} > Remove </button>
</td>
</tr>
);
}
});

var IncludeProducts = React.createClass({
render:function(){
return (
<tr>
<td>
Filter Products (Includes)
</td>
<td>
Exclude Products Where <SelectOptions options={options} />
</td>
<td>
does not equals <input type="text" name="" />
</td>
<td>
<button className="btn btn-danger" onClick={this.props.removeOperation} > Remove </button>
</td>
</tr>
);
}
});

var DynamicFields = React.createClass({
getInitialState:function(){
return {
operations:[]
}
},
removeOperation:function(index){
var operations = this.state.operations;
operations.splice(index,1);
this.setState({operations:operations});
},
addOperation:function(){
var value = this.refs.operationsDropDown.value;
this.setState({operations:this.state.operations.concat([value])});
},
renderAdjustmentRows:function(){

fields = this.state.operations.map((operation,index) =>{
if(operation == "adjust-price-multiply"){
return (<PriceMultiply key={index} removeOperation={this.removeOperation} />);
}else if(operation == "adjust-price-add"){
return (<PriceAdd key={index} removeOperation={this.removeOperation} />);
}else if(operation == "filter-products-includes"){
return (<IncludeProducts key={index} removeOperation={this.removeOperation} />);
}else if(operation == "filter-products-excludes"){
return (<ExcludeProducts key={index} removeOperation={this.removeOperation} />);
}
});

return (
<table>
<tbody>
{fields}
</tbody>
</table>
);
},
render:function(){
return (
<div>
<select className="browser-default" ref="operationsDropDown" id="operations-drop-down">
<option value="adjust-price-multiply"> Adjust Price (Multiply) </option>
<option value="adjust-price-add"> Adjust Price (Add) </option>
<option value="filter-products-includes"> Filter products (Includes) </option>
<option value="filter-products-excludes"> Filter products excludes </option>
</select>
<button onClick={this.addOperation} > Add Operation </button>
<div id="adjust-import-data-rows">
{this.renderAdjustmentRows()}
</div>
</div>
);
}
});

ReactDOM.render(<DynamicFields />,document.getElementById('container'));

最佳答案

问题是您没有为 removeOperation 函数提供索引。在发送索引之前,您的组件应该具有索引。因此,使用索引属性传递它,如下所示:

renderAdjustmentRows:function(){

   fields = this.state.operations.map((operation,index) =>{
if(operation == "adjust-price-multiply"){
return (<PriceMultiply key={index} index={index} removeOperation={this.removeOperation} />);
}else if(operation == "adjust-price-add"){
return (<PriceAdd key={index} index={index} removeOperation={this.removeOperation} />);
}else if(operation == "filter-products-includes"){
return (<IncludeProducts key={index} index={index} removeOperation={this.removeOperation} />);
}else if(operation == "filter-products-excludes"){
return (<ExcludeProducts key={index} index={index} removeOperation={this.removeOperation} />);
}
});

并通过每个动态组件的删除按钮单击发送索引 Prop 。为此,将所有删除按钮替换为

 <button className="btn btn-danger" onClick={this.props.removeOperation.bind(null, this.props.index)} > Remove </button>

你会得到你想要的。

完整代码可在https://jsfiddle.net/KishoreBarik/f8h3c82m/获取

关于javascript - 在 React Js 中添加和删除输入字段,且值不会重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37176412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com