如何在功能之外移动变量

  • 本文关键字:移动 变量 功能 jquery
  • 更新时间 :
  • 英文 :


这是我的代码:

function type_of_value (){
 $(".typ_wartosci").each(function Type(){
  a = $(this).find(':selected').text();
})
}
alert(new type_of_value().a);

我想显示"一个"变量,但我不知道如何。我尝试在函数之前设置变量,并使用" .push"方法,但行不通。我没有一个主意,下一步该怎么办。

您可以像这样返回a -

function type_of_value (){
 let a = []
  $(".typ_wartosci").each(function(){
    var _this = $(this)
    if(_this.find(':selected')) {
      a.push(_this.find(':selected').text())
    }
  })
  return a
}
alert(new type_of_value())

如果您确定单个选择值,则 -

function type_of_value (){
 let a
  $(".typ_wartosci").each(function(){
    var _this = $(this)
    if(_this.find(':selected').text()) {
      a = _this.find(':selected').text()
    }
  })
  return a
}
alert(new type_of_value())

最新更新