選択したイベントに基づいて各テキストボックスにselectboxのテキストを表示するにはどうすればよいですか? 私のコードはselectboxの値をテキストボックスにのみ表示します。私はそれを各オプションのテキストに置き換えたいと思う。selectboxのテキストをテキストボックスに表示するにはどうすればよいですか?
var selectBox = document.getElementById('mySelect');
var selectBox2 = document.getElementById('mySelect2');
selectBox.addEventListener('change', handleChange);
selectBox2.addEventListener('change', handleChange);
function handleChange(event)
{
if (event.target.id == "mySelect") {
document.getElementById('myInput').value = selectBox.value;
} else {
document.getElementById('myInput2').value = selectBox2.value;
}
}
<select id="mySelect">
<option value="Option 1"> this Text</option>
<option value="Option 2">this Text 2</option>
</select>
<select id="mySelect2">
<option value="Option 1">this Text</option>
<option value="Option 2">this Text 2 </option>
</select>
<input type="text" id="myInput" />
<input type="text" id="myInput2" />
を
.text
を変更したいのでありがとうございました。あなたの質問があります。どのように私は選択ボックスを変更する前に、テキストボックスで最初の結果を表示できますか? –私は答えを更新しました。基本的には、ページが読み込まれるときに初期値をチェックし、それに応じてテキストボックスに設定する必要があります。理想的には、それを[document.ready](https://learn.jquery.com/using-jquery-core/document-ready/)にラップします。 – Nisarg