4

inputユーザーが「テキスト2」と入力すると、フォームフィールドで「テキスト2」が選択されselectます。

<select id="formsel">
    <option value="text 1">text 1</option>
    <option value="text 3">text 2</option>
    <option value="text 3">text 3</option>
</select>
<input type='text' id='input' />

次のように値を取得しますinput

var input_val = document.getElementById('input').value; 

しかし、動的フォームからオプションを選択できませselect

document.form.formsel.value = input_val;

誰かが私が間違っていることを見ることができますか?

4

4 に答える 4

3

あなたのコードはformあなたがアクセスしようとしていることを示していないdocument.formので、私はフォームがないと仮定しています。selectIDでにアクセスしてみてください。これは私にとってはうまくいくようです:

<script>
document.getElementById('input').onkeyup = function()
{
    var input_val = document.getElementById('input').value;
    document.getElementById('formsel').value = input_val;
}
</script>
于 2012-11-01T15:52:33.727 に答える
1

コメントに基づくと、jQueryを使用しているようです(その場合は、すべての質問にjQueryのタグを付ける必要があります)。

これはあなたが望むものを手に入れるはずです

var selectedText = $("#formsel option:selected").text()
于 2012-11-01T15:55:02.777 に答える
0

これを試して:

var nodes = document.getElementById('formsel'),
    txt = document.getElementById('input').value, node;

for ( var i = nodes.length; i--; ) {

    node = nodes[i];

    if ( txt === nodes[i].value ) {

        node.selected = true; break;

    }

}
于 2012-11-01T15:56:21.963 に答える
0

タイプミスがあるようです。

<option value="text 3">text 2</option>

次のようにする必要があります。

<option value="text 2">text 2</option>
于 2012-11-01T15:48:00.207 に答える