2

JS/jQuery を使用して、これを行う最善の方法は何ですか?

私は5つの変数を持っています:

var fruit1 = "apple";
var fruit2 = "mango";
var fruit3 = "orange";
var fruit4 = "banana";
var fruit5 = "melon";

次に、クリックされた特定の果物を含むリスト要素があります。

<li id="banana">Banana</li>

スクリプトでその ID を取得します。

$("li").on("click", function() {
    var selectedfruit = $(this).attr("id");
});

変数リストと一致selectedfruitさせて、何かできることを返すにはどうすればよいfruit4ですか?

二次的な質問ですが、変数リストを配列に入れる必要がありますか?

どうもありがとう。

編集:大変申し訳ありませんが、大きな間違いを犯しました

変数の内容ではなく、selectedfruit変数で検証する必要があります。

したがって、マークアップは次のようになります。

<li id="fruit4">Mystery fruit</li>

4

7 に答える 7

8

Yes, this is definitely a job for an array.

var fruits = ["apple", "mango", "orange", "banana", "melon"];

Then in your click handler, you can search this array, and get its index.

$("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)
});

UPDATE Based off of your update, I would use an object instead.

var fruits = {
    fruit1: "apple",
    fruit2: "mango",
    fruit3: "orange",
    fruit4: "banana",
    fruit5: "melon"
};

Then you can use the ID to get the value and compare.

$("li").on("click", function() {
    var selectedfruit = fruits[$(this).attr("id")];  // banana
});
于 2012-08-16T17:14:09.200 に答える
3

個々の変数の代わりにJavaScript配列を使用できますか?

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

このJsFiddleを参照してください

于 2012-08-16T17:16:19.043 に答える
3

以下のようなものを試すことができます:

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"],
    selectedfruit  = $(this).attr("id");

$("li").on("click", function() {
    var index = $.inArray( fruits, selectedfruit  );
    if( index >= 0 ) // checking that fruit exists in the array, if not index === -1
       var fruitIndex = 'fruit' + (index + 1);
});
于 2012-08-16T17:13:35.893 に答える
2

はい、変数を配列に入れる必要があります。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. 
     });     
于 2012-08-16T17:17:44.753 に答える
2

変数を配列にしてみてください。

var fruits = ["apple", "mango", "orange", "banana", "melon"];

次に、次を使用して配列のインデックスを取得しますindexOf()

$("li").on("click", function() {
    var fruitIndex = fruits.indexOf($(this).attr("id"));
});

一部のブラウザは をサポートしていないため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;
    }
}

次に、クリックしたフルーツ要素を次のように操作できます。

alert(fruits[fruitIndex]);
于 2012-08-16T17:13:48.903 に答える
2

変数を配列に格納すると、はるかに優れたものになります。その後、 を使用して簡単に一致を見つけることができますjQuery.inArray

于 2012-08-16T17:14:15.630 に答える
1

彼には構文のヘルプが必要だと思いました。現在更新されています。より良い答えはすでに存在します:

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");
        }
        }
    });​
于 2012-08-16T17:22:45.133 に答える