Jquery 将字符串与存储变量列表相匹配

Jquery 将字符串与存储变量列表相匹配,jquery,variables,pattern-matching,Jquery,Variables,Pattern Matching,使用JS/jQuery,最好的方法是什么 我有5个变量: var fruit1 = "apple"; var fruit2 = "mango"; var fruit3 = "orange"; var fruit4 = "banana"; var fruit5 = "melon"; 然后我有一个列表元素,其中有一个被点击的水果: <li id="banana">Banana</li> 如何将selectedfruit与变量列表匹配,以便它返回fruit4,我可以用它来处

使用JS/jQuery,最好的方法是什么

我有5个变量:

var fruit1 = "apple";
var fruit2 = "mango";
var fruit3 = "orange";
var fruit4 = "banana";
var fruit5 = "melon";
然后我有一个列表元素,其中有一个被点击的水果:

<li id="banana">Banana</li>
如何将
selectedfruit
与变量列表匹配,以便它返回
fruit4
,我可以用它来处理一些事情

第二个问题,我应该将变量列表放入数组中吗

非常感谢

编辑:非常抱歉,我犯了一个巨大的错误

我需要使用变量name而不是变量contents来验证
selectedfruit

因此,标记将如下所示:

alert(fruits[fruitIndex]);

  • 神秘水果
  • 您可以尝试以下方法:

    var fruits = {
      fruit1 : "apple",
      fruit2 : "mango",
      fruit3 : "orange",
      fruit4 : "banana",
      fruit5 : "melon"
    };
    
    $("li").on("click", function() {
        var selectedfruit = $(this).attr("id");
        alert( fruits[selectedfruit] );
    });
    
    但是使用数组
    尝试将变量设置为数组:

    var fruits = ["apple", "mango", "orange", "banana", "melon"];
    
    然后使用
    indexOf()
    获取数组的索引:

    由于某些浏览器不支持
    indexOf()
    ,因此可以使用以下方法强制支持它们:

    if (!Array.prototype.indexOf) { 
        Array.prototype.indexOf = function(obj, start) {
             for (var i = (start || 0), j = this.length; i < j; i++) {
                 if (this[i] === obj) { return i; }
             }
             return -1;
        }
    }
    

    是的,这绝对是阵列的工作

    var fruits = ["apple", "mango", "orange", "banana", "melon"];
    
    然后在单击处理程序中,可以搜索此数组并获取其索引

    $("li").on("click", function() {
        // Note: This will return -1, if it's not in the array
        var selectedfruit = $.inArray($(this).attr("id"), fruits); // 3 (arrays are zero-indexed)
    });
    
    更新基于您的更新,我将使用对象

    var fruits = {
        fruit1: "apple",
        fruit2: "mango",
        fruit3: "orange",
        fruit4: "banana",
        fruit5: "melon"
    };
    
    然后,您可以使用ID获取值并进行比较

    $("li").on("click", function() {
        var selectedfruit = fruits[$(this).attr("id")];  // banana
    });
    

    如果将变量存储在数组中会更好。然后您可以使用jQuery轻松找到匹配项。inArray

    您可以使用javascript数组而不是单个变量吗

    var fruits=new Array("apple","mango","orange", "banana", "melon");
    $.inArray("mango", fruits)
    

    参见此

    是的,您应该将变量放入数组中。在jquery中编写代码已经有一段时间了,但我想我可以尝试一下:

         var fruits[] = new Array(your variable values)
         $(document).ready(function() {
              //click handler event and then take the value of id attribute as you mentioned
              //iterate over the loop and then store index and do something with that if you 
              //want. 
         });     
    

    我认为他需要一些语法帮助;现在更新。更好的答案已经存在:

    var fruits = ["apple", "mango", "orange", "banana", "melon"];
    $("li").click(function() {
        var selectedfruit = $(this).attr("id");
        for (i = 0; i < fruits.length; i++) {
            if (selectedfruit == "fruit" + (i+1)) {
                alert(fruits[i] + " is pressed");
            }
            }
        });​
    
    var水果=[“苹果”、“芒果”、“橙子”、“香蕉”、“甜瓜”];
    $(“li”)。单击(函数(){
    var selectedfruit=$(this.attr(“id”);
    对于(i=0;i
    @Rocket-很好。我编辑了这篇文章来解决这个问题。谢谢你的回复。由于我的一个错误,我更新了我的问题。Rocket和我已经决定用一个对象最好,但我很感谢你的帮助。谢谢你没有错过我的编辑。你的目标想法非常适合我要做的。我的代码正是我的更新描述的,现在感谢你,它是功能性的。谢谢你的回复。使用数组和对象有什么好处吗?谢谢你的回复。我认为一个对象更适合我正在尝试做的事情。这不起作用,因为用户可以单击
    水果3
    或任何其他水果。
    var fruits=new Array("apple","mango","orange", "banana", "melon");
    $.inArray("mango", fruits)
    
         var fruits[] = new Array(your variable values)
         $(document).ready(function() {
              //click handler event and then take the value of id attribute as you mentioned
              //iterate over the loop and then store index and do something with that if you 
              //want. 
         });     
    
    var fruits = ["apple", "mango", "orange", "banana", "melon"];
    $("li").click(function() {
        var selectedfruit = $(this).attr("id");
        for (i = 0; i < fruits.length; i++) {
            if (selectedfruit == "fruit" + (i+1)) {
                alert(fruits[i] + " is pressed");
            }
            }
        });​