0
私はユーザーが複数の添付ファイルを添付できる反応ファイルアップローダーを持っています。ユーザが入力をクリックするたびに、data-index
を読んで入力の位置を確認します。evt.targetは常に入力リストの最初の入力を返します
renderFileUploader() {
let file_attachment = this.state.file_attachment.map(function (b, i) {
console.log("Checking the value of i in here");
console.log(i); /// Shows expected values (0..x)
return (
<li key={i}>
<div className="attach-file-input">
<div className={b[3]}>
<label htmlFor="file_attachment"><i className={b[4]}></i></label>
<input type="file" name="file_attachment" data-index={i} data-type="file_attachment" id="file_attachment" onChange={this.upload.bind(this)} />
<p>{b[2]}</p>
</div>
<div><a href="#" onClick={this.updateList} data-type="file_attachment" data-index={i} data-delete="true" className="remove-icon">×</a></div>
<span className="uploaded-alert">{b[1]}</span>
</div>
</li>
)
}.bind(this));
return(
<div className="form-group file-attachment-area">
<ul className="add-files">{file_attachment}</ul>
<a onClick={this.updateList} data-type="file_attachment" className="text-right add-attachment">+</a>
</div>
);
}
しかし、私はドキュメントをアップロードし、upload
機能は、それが常に0
としてインデックス値をとり、したがって、第一の位置に要素を更新し続け、その時点で呼び出されたとき。
upload(evt, i) {
evt.preventDefault();
console.log(evt.target); // Console log to check position
....
}
任意の要素を選択する上で、私のコンソールログには、次のように出力されます
<input type="file" name="file_attachment" data-index="0" data-type="file_attachment" id="file_attachment">
だから、データ・インデックスは常に0
EDITです:修正されたコード
<label htmlFor={"file_attachment" + [i]}><i className={b[4]}></i></label>
<input type="file" name="file_attachment" data-index={i} data-type="file_attachment" id={"file_attachment" + [i]} onChange={this.upload.bind(this)} />
は 'データ・インデックスは= 0 'データ - 'ではないべきではありませんindex = "0" '? – Red
問題を引き起こしているのは、各要素の 'id'値を同じ値に設定していて、ID値が一意でなければならないようです。 –
@RichardMauritzごめんなさい、それはタイプミスでした。私はそれを修正するために質問を編集しました – anonn023432