在函数内打印一个js变量

时间:2016-05-26 17:49:48

标签: javascript jquery angularjs

我希望获取值onclick并将其打印在符号"symbol": +sym+中, 。 检查我的代码:

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

</head>
<body>
<p>click here</p>

  <!-- TradingView Widget BEGIN -->
  <script type="text/javascript" src="https://d33t3vvu2t2yu5.cloudfront.net/tv.js"></script>
  <script type="text/javascript">
$(document).ready(function(){    
      $("p").click(function(){
          var sym = $(this).val();
          alert("alert is working but value is not updating in symbol input");
      });
  });

  new TradingView.widget({
    "width": 400,
    "height": 450,
    "symbol": +sym+,
    "interval": "D",
    "timezone": "Etc/UTC",
    "theme": "White",
    "style": "1",
    "locale": "en",
    "toolbar_bg": "#f1f3f6",
    "allow_symbol_change": true,
    "hideideas": true,
    "show_popup_button": true,
    "popup_width": "1000",
    "popup_height": "650",
    "no_referral_id": true
  });
  </script>
  <!-- TradingView Widget END -->


</body>
</html>

3 个答案:

答案 0 :(得分:2)

你需要改变:

Map<Group, List<Person>>

到此:

var sym = $(this).val();

或者这个:

var sym = $(this).html();

取决于你想要得到什么。您可能知道var sym = $(this).text(); 代码不包含p属性。

答案 1 :(得分:2)

首先,您需要将此 "symbol": +sym+ 更改为: "symbol": sym, 。如果你想要实现的是改变变量的价值&#34; sym&#34;在新的&#34; TreadingView.widget&#34;每当你点击段落&#34; p&#34;你做错了。您需要在每次单击元素时更新该值,否则小部件对象将始终保存变量的原始值&#34; sym&#34;,您需要1)更新&#34; sym&#34;值,然后2)重新创建小部件或更新,您可以使用:

    <!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

</head>
<body>
<p>click here</p>

  <!-- TradingView Widget BEGIN -->
  <script type="text/javascript" src="https://d33t3vvu2t2yu5.cloudfront.net/tv.js"></script>
  <script type="text/javascript">
$(document).ready(function(){    
      $("p").click(function(){
          var sym = $(this).text();
          new TradingView.widget({
              "width": 400,
              "height": 450,
              "symbol": sym,
              "interval": "D",
              "timezone": "Etc/UTC",
              "theme": "White",
              "style": "1",
              "locale": "en",
              "toolbar_bg": "#f1f3f6",
              "allow_symbol_change": true,
              "hideideas": true,
              "show_popup_button": true,
              "popup_width": "1000",
              "popup_height": "650",
              "no_referral_id": true
           });
          alert("alert is working but value is not updating in symbol input");
      });
  });

  </script>
  <!-- TradingView Widget END -->


</body>
</html>

答案 2 :(得分:1)

试试这个

       <script type="text/javascript">
               var sym = null;
               $(document).ready(function(){    
                 $("p").click(function(){
              sym = $(this).text();
              alert("alert is working but value is not updating in symbol input");
          });
         });
         setTimeout(function(){
   sym = $(this).text();
new TradingView.widget({
          "width": 400,
         "height": 450,
         "symbol": +sym+,
         "interval": "D",
        "timezone": "Etc/UTC",
        "theme": "White",
         "style": "1",
          "locale": "en",
        "toolbar_bg": "#f1f3f6",
        "allow_symbol_change": true,
        "hideideas": true,
        "show_popup_button": true,
        "popup_width": "1000",
        "popup_height": "650",
        "no_referral_id": true
        });},5000);

         </script>
相关问题