0

チェックボックスが大部分を占めるフォームがあります。チェックボックスにはそれぞれ、JavaScript配列内のアイテムに対応するID「値」があります。配列アイテムは、テキストエリアに入力するテキストを保持します。

サイトをクリーンアップするためのドロップダウンボックスをいくつか含めたいと思います。ただし、ドロップダウンオプションに配列IDを割り当てることができないようです。これはselectboxオプションで実行できますか?タブを使用せずに選択ボックスをシミュレートするための回避策はありますか?

私のhtmlは基本的に:

    <div>
    <input type=checkbox id=array1 name=textArray></input>
    <input type=checkbox id=array1 name=textArray></input>
    <input type=checkbox id=array1 name=textArray></input>
    ...
    <select><option 1><option 2>...</select>
    </div>
    <div>
    <form>
    <textarea id=outPut></textarea>
    </form>
    </div>

そして私のjsは:

var textArray = {

array1: 'some text here',
array2: 'some more text',
array3: 'some other text',
...
array90: 'the last text'

};

// variable assigned to chosen item
var selectedInputs = document.getElementsByName("textArray");
for (var i = 0; i < selectedInputs.length; i++) {
    selectedInputs[i].onchange = function() {
        chosenItem = this;
        printText();        
    };
}
// Script to add items to the Comments section text area
var mytextbox = document.getElementById('outPut');
var chosenItem = null;

function printText(){
    if(chosenItem !== null){
       mytextbox.value += textArray[chosenItem.id] + "";
        // resets the radio box values after output is displayed
        chosenItem.checked = false;
        // resets these variables to the null state
        chosenItem = null;
    }
}

js配列内のアイテムを選択ボックスの選択肢の1つに関連付けるにはどうすればよいですか?

4

2 に答える 2

1

あなたが何を求めているのか理解するのは非常に難しいと思いましたが、私はこれを一緒に投げました、そしてうまくいけばそれが役立つでしょう。

重要なビットは

var selectNode = document.getElementById('select'); // <select id="select">
selectNode.onchange = function () {
  if (selectNode.selectedIndex !== 0) {
    chosenItem = selectNode.options[selectNode.selectedIndex];
    selectNode.selectedIndex = 0;
    printText();
  }
}

そして、あなたがしていることにid属性を使用しないでください(私はdata-iを使用しました)。


また、コードをクリーンアップする場合は、関数間で変数を渡す方法を強く再検討する良い機会になると思います。グローバル名前空間に値を設定し、次の呼び出しでその値に依存することは、問題(競合状態、他のコードとの競合など)を要求するだけです。

于 2013-01-08T19:25:49.390 に答える
0

<option value="whatever">1</option>これは最初からHTMLの一部です。

于 2013-01-08T19:10:53.983 に答える