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>