Jquery/html-检查所选选项是否在4个数组中的一个数组中,然后执行函数

Jquery/html-检查所选选项是否在4个数组中的一个数组中,然后执行函数,jquery,html,forms,Jquery,Html,Forms,我已经在网上/其他问题上寻找了这方面的帮助,但是我不能完全理解,并且假设我没有使用正确的关键字 我试图根据用户从网络表单上的下拉菜单中选择的值来做一些事情。逻辑是, 用户从下拉列表中选择一个选项 脚本检查该选项是否在数组1、2、3、4中 根据它所在的数组,它会显示不同的HTML div 这听起来很简单,但我一点也不知道从哪里开始。感谢您一如既往的帮助。快速完成此任务,因此我相信它可以改进 我想到两种方法,第一种是使用.indexOf: <script type="text/javasc

我已经在网上/其他问题上寻找了这方面的帮助,但是我不能完全理解,并且假设我没有使用正确的关键字

我试图根据用户从网络表单上的下拉菜单中选择的值来做一些事情。逻辑是,

  • 用户从下拉列表中选择一个选项
  • 脚本检查该选项是否在数组1、2、3、4中
  • 根据它所在的数组,它会显示不同的HTML div

这听起来很简单,但我一点也不知道从哪里开始。感谢您一如既往的帮助。

快速完成此任务,因此我相信它可以改进

我想到两种方法,第一种是使用
.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>

.支持指数 注意: 并非所有浏览器都支持.indexOf()(特别是IE)。 因此,如果您想使用indexOf(),首先应该使用下面的代码,以便在所有浏览器中添加对它的支持(代码归功于mozilla)


if(!Array.prototype.indexOf)
{
Array.prototype.indexOf=函数(elt/*,from*/)
{
var len=this.length>>>0;
var from=Number(参数[1])| | 0;
from=(from<0)
?数学单元(来自)
:数学。地板(从);
如果(从<0开始)
from+=len;
for(;from
if(input==arr1){…}else if(input==arr2){…
switch(input){case arr1:…break;case arr2:…}
查找数组中的值的一些帮助:要获得最快的答案,请尝试查看此页面中发布的
相关
&
链接的
链接的答案。此问题已被问了很多次,我觉得没有必要再写了。由于我对您的要求有点误解,请发布答案。很抱歉延迟回复,这是非常重要的非常好,非常感谢:-)
<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>
<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>