首页 > 解决方案 > 是否可以在 Jquery 中使用接收到的值中的“变量”?

问题描述

我有一个选择输入和以下功能:

$(document).on('change','select#country',function(){
var Netherlands = "Code: NL. A currency in Netherlands is Euro (EUR)";
var Poland = "Code: PL. A currency in Poland is Polish Zloty (PLN)";
var xd = $(this).find('option:selected').text()
alert(xd) //use a var from received text (which equal to var)
})

我想提醒变量下的信息。

因此,改为警告标签文本(例如波兰)我想使用这个名称下的可验证(返回有关某个国家/地区的信息)。

有可能做到吗?

先感谢您。

标签: jquery

解决方案


是的你可以 !我建议使用 object type 。所以尝试如下

$(document).on('change','select#country',function(){
var CodeList = {
  Netherlands : "Code: NL. A currency in Netherlands is Euro (EUR)",
  Poland      : "Code: PL. A currency in Poland is Polish Zloty (PLN)"
}
var xd = $(this).find('option:selected').text()
alert(CodeList[xd]) //use a var from received text (which equal to var)
})

推荐阅读