简体   繁体   English

在更改事件中为现有的JavaScript代码增加价值

[英]add value to existing javascript code on change event

i have a javascript widget that will display a data chart.. and i have an dropdown box which gives the user the ability to change the theme. 我有一个将显示数据图表的JavaScript小部件。.我还有一个下拉框,使用户可以更改主题。 so when the user selects a theme it should change and show the user the new theme. 因此,当用户选择主题时,它应该更改并向用户显示新主题。

Widget code: 小部件代码:

<script type="text/javascript">
new TradingView.widget({
  "width": 500,
  "height": 400,
  "interval": "1",
  "timezone": "Etc/UTC",
  "theme": "White",
  "style": "2",

}); 
</script>

HTML/JS code HTML / JS代码

<select>
    <option value="moonlight">Moon</option>
    <option value="darkness">Dark</option>
</select>

<script>
$(document).on('change','.theme',function(){
      alert(this.value);
    });
</script>

How do i get add the this.value to the widget JS code theme parameter? 我如何将this.value添加到小部件JS代码theme参数中?

I guess you should put the new widget code in a function and access the theme from the args: 我想您应该将新的窗口小部件代码放入函数中,并从args访问theme

<script type="text/javascript">
function themeChanger(theme){
  var themeWidget = new TradingView.widget({
    "width": 500,
    "height": 400,
    "interval": "1",
    "timezone": "Etc/UTC",
    "theme": theme,
    "style": "2",

  }); 
  return themeWidget;
}

$(document).on('change','.theme',function(){
   var widget = themeChanger(this.value);
   // do something with widget.
});

</script>

you can use global variable 您可以使用全局变量

var chartParams={
  "width": 500,
  "height": 400,
  "interval": "1",
  "timezone": "Etc/UTC",
  "theme": "White",
  "style": "2",

}
new TradingView.widget(chartParams);
$(document).on('change','.theme',function(){
     chartParams["theme"]=this.value
     new TradingView.widget(chartParams);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM