2

私はこれについての助けを求めてウェブ/その他の質問を見回しましたが、それを完全に理解することはできず、正しいキーワードを使用していないと思います.

ユーザーが Web フォームのドロップダウン メニューから選択した値に基づいて何かをしようとしています。ロジックは次のとおりです。

  • ユーザーがドロップダウンからオプションを選択します
  • スクリプトは、そのオプションが配列 1、2、3、4 のいずれかにあるかどうかをチェックします。
  • 含まれている配列に応じて、異なる HTML div が表示されます

頭の中では単純に聞こえますが、どこから始めればよいかまったくわかりません。いつものように助けに感謝します。

4

1 に答える 1

0

これを本当に素早く行うので、改善できると確信しています。

私の頭に浮かぶ2つの方法があります、最初は使用するでしょう.indexOf

<script type="text/javascript>
    $(document).ready(function()
    {
        var selectedVal = 'asd';
        var selectedIndex;

        var arrayHolder; //should hold the array the value was found in
        var arr1 = ['huh', 'asd'];
        var arr2 = ['asdasd'];
        var arr3 = ['asdasdasd'];
        var arr4 = ['asdasdasdasd'];

        //arr1
        arrayHolder = arr1;
        selectedIndex = arrayHolder.indexOf('asd');

        if (!selectedIndex)
        {
            //arr2
            arrayHolder = arr2;
            selectedIndex = arrayHolder.indexOf('asd');

            if (!selectedIndex)
            {
                //arr3
                arrayHolder = arr3;
                selectedIndex = arrayHolder.indexOf('asd');

                if (!selectedIndex)
                {
                    //arr4
                    arrayHolder = arr4;
                    selectedIndex = arrayHolder.indexOf('asd');

                    if (!selectedIndex)
                    {
                        alert(selectedVal + ' not found in all 4 arrays.');
                    }
                }
            }
        }

        //By here, you should know if a value was found and in which array
        alert(arrayHolder);//contains the array in which the value was found
        alert(selectedIndex);//The index number where it was found

        //test
        console.log(arrayHolder[selectedIndex]);
    });
</script>

2番目の方法は、jQueryを使用してそれを行うことですinArray

<script type="text/javascript>
    $(document).ready(function()
    {
        var selectedVal = 'asd';
        var selectedIndex;

        var arrayHolder; //should hold the array the value was found in
        var arr1 = ['huh', 'asd'];
        var arr2 = ['asdasd'];
        var arr3 = ['asdasdasd'];
        var arr4 = ['asdasdasdasd'];

        //check first array
        arrayHolder = arr1;
        selectedIndex = $.inArray(selectedVal, arrayHolder);

        //check if found in first array, if not check next array
        if (!selectedIndex)
        {
            arrayHolder = arr2;
            selectedIndex = $.inArray(selectedVal, arrayHolder);

            //check if found in second array, if not check next array
            if (!selectedIndex)
            {
                arrayHolder = arr3;
                selectedIndex = $.inArray(selectedVal, arrayHolder);

                //check if found in third array, if not check next array
                if (!selectedIndex)
                {
                    arrayHolder = arr4;
                    selectedIndex = $.inArray(selectedVal, arrayHolder);

                    //check if found in third array, if not check next array
                    if (!selectedIndex)
                    {
                        alert(selectedVal + ' not found in all 4 arrays.');
                    }
                }
            }
        }

        //By here, you should know if a value was found and in which array
        alert(arrayHolder);//contains the array in which the value was found
        alert(selectedIndex);//The index number where it was found

        //test
        console.log(arrayHolder[selectedIndex]);
    });
</script>

.indexOfサポート

注: .indexOf()は、すべてのブラウザーでサポートされているわけではありません(特に、IEの場合)。したがって、indexOf()を使用する場合は、まず以下のコードを使用して、すべてのブラウザーでサポートを追加する必要があります(コードのmozillaへのクレジット)。

<script type="text/javascript">
        if (!Array.prototype.indexOf)
        {
          Array.prototype.indexOf = function(elt /*, from*/)
          {
            var len = this.length >>> 0;

            var from = Number(arguments[1]) || 0;
            from = (from < 0)
                 ? Math.ceil(from)
                 : Math.floor(from);
            if (from < 0)
              from += len;

            for (; from < len; from++)
            {
              if (from in this &&
                  this[from] === elt)
                return from;
            }
            return -1;
          };
        }
</script>
于 2013-02-13T04:09:06.517 に答える