代码之家  ›  专栏  ›  技术社区  ›  kibus90

可以从Jquery中的接收值使用“variables”吗?

  •  -2
  • kibus90  · 技术社区  · 5 年前

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

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

    我想提醒一下变量下面的信息。

    所以,我想使用这个名字下的veriable(返回某个国家的信息)。

    有可能吗?

    1 回复  |  直到 5 年前
        1
  •  2
  •   David Japan    5 年前

    是的,你可以!我建议使用对象类型。所以试试下面的

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