により、すべての配列値1にアクセスすることができましたそれぞれの名前彼らはそれぞれ排出されるまで:
// Your array
var array = ['Tom', 'Matt', 'Lucy', 'Suzanna', 'Hank'];
// Loop through the array and target the next available textbox
for(var input in document.getElementsByName('firstName')){
// If there are any names to use, use one
if(array.length > 0){
// Pop the next name off of your array and set the value
// of your textbox
input.value = array.pop();
}
}
あなたが実際に値を設定するには、上記の例を使用してすべての問題を持っている場合、あなたは常に物事を処理するために、わずかに異なるループを使用することができます。
// Your array
var array = ['Tom', 'Matt', 'Lucy', 'Suzanna', 'Hank'];
// Store your input elements
var inputs = document.getElementsByName('firstName');
// Loop through the array and target the next available textbox
for(var i = 0; i < inputs.length; i++){
// If there are any names to use, use one
if(array.length > 0){
// Pop the next name off of your array and set the value
// of your textbox
inputs[i].value = array.pop();
}
}
あなたがsee a working example in action hereできると何が提供されたデータを使用して、出力は以下のようになります。
入力をループし、配列から次の項目を追加する関数を記述します。 – WillardSolutions