繁体   English   中英

单击Rails中的按钮后如何在JQuery中调用函数

[英]How to call a function in JQuery after clicking a button in Rails

我想在用户单击按钮之后绘制( id='dd'

<%= image_tag("logo.png", size: "460x69", alt: "hallo", class: "logo") %>
<h1 class="main_title"><center>WOrld</center></h1>


<div >
  <p class="mod1_title">Modul</p>
  <div class="mod1_boxed">
    <strong>Energy (kWh):</strong> <a href="#"><img src="infoicon.png" class="info"></img></a>
      <%= form_tag( '/calculation/index', post: true ) do %>
      <%= text_field_tag "user_entry_module1", nil, placeholder: "Bsp. 3500", id: "user_entry" %></br>

      <strong>PV (kWp):</strong></br>
      <%= text_field_tag "user_entry_module2", nil, placeholder: "Bsp. 5", id: "user_entry_2" %></br>
      <%= submit_tag "Berechnen", id: "dd"%>

      <%end%>
  </div>  
</div>

<div id="container" style="min-width: 410px; max-width: 800px; height: 400px; margin-left: 150px; margin-top: 0px;"></div>




<%-# Reading data from the Front-End -%>
<%= javascript_tag do %>
  window.a = '<%= params[:user_entry_module1].to_i %>';
  window.b = '<%= params[:user_entry_module2].to_i %>';  
<% end %>



<script >

  $(function(){

    // Converting entries by user to integer
    var myInteger1; 
    myInteger1 = parseInt(a); 
    var myInteger2; 
    myInteger2 = parseInt(b);
    sum = myInteger1 + myInteger2; 
    console.log("Summe von entries:"+sum);




    // Calling my API with AJAX
    $('#dd').click(function(){

      $.ajax({ 
      type: 'POST', 
      url: '/api/v2/energycalcsb', 
      data: {"data":[myInteger1,myInteger2]}, 
      dataType: 'json', 
      success: function(data){ //Sending the output to a function
        send_to_front(data); 
        //console.log('success', data); //Help to print the output of the API
      }      

      });
       });

      // Function which receives the data from AJAX and the plot them with Highcharts    
      function send_to_front(data){ 
        var outputs = ['discharge_kWh', 'charge_kWh', 'purchase_kWh', 'feeding_kWh', 'own_consumption_kWh', 'own_production_kWh'];
        var eco4 = new Array;
        var eco6 = new Array;
        var eco8 = new Array;
        var eco10 = new Array;
        var eco12 = new Array;
        var eco14 = new Array;
        var eco16 = new Array;

        for (var i=0;i<=5;i++)
        {
          eco4[i] = data['data_output']['eco4'][outputs[i]];
          eco6[i] = data['data_output']['eco6'][outputs[i]];
          eco8[i] = data['data_output']['eco8'][outputs[i]];
          eco10[i] = data['data_output']['eco10'][outputs[i]];
          eco12[i] = data['data_output']['eco12'][outputs[i]];
          eco14[i] = data['data_output']['eco14'][outputs[i]];
          eco16[i] = data['data_output']['eco16'][outputs[i]];
        }


           $('#container').highcharts({
               chart: {
                   type: 'column'
               },
               title: {
                   text: 'Modul'
               },

               xAxis: {
                   categories: ['eco4', 'eco6', 'eco8', 'eco10', 'eco12', 'eco14', 'eco16'],
                   title: {
                       text: null
                   }
               },
               yAxis: {
                   min: 0,
                   title: {
                       text: 'kWh',
                       align: 'high'
                   },
                   labels: {
                       overflow: 'justify'
                   }
               },
               tooltip: {
                   valueSuffix: ' kWh'
               },
               plotOptions: {
                   bar: {
                       dataLabels: {
                           enabled: true
                       }
                   },

               //series: {
                 //pointPadding:0.02
                   //     }
               },

               legend: {
                   layout: 'vertical',
                   align: 'right',
                   verticalAlign: 'top',
                   x: -20,
                   y: 50,
                   floating: true,
                   borderWidth: 1,
                   backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
                   shadow: true
               },
               credits: {
                  enabled: false
               },
               series: [{
                   name: 'discharge',
                   data: [eco4[0],eco6[0],eco8[0],eco10[0],eco12[0],eco14[0],eco16[0]]
               }, {
                   name: 'charge',
                   data: [eco4[1],eco6[1],eco8[1],eco10[1],eco12[1],eco14[1],eco16[1]]
               }, {
                   name: 'purchase',
                   data: [eco4[2],eco6[2],eco8[2],eco10[2],eco12[2],eco14[2],eco16[2]]
               },{
                   name: 'feeding',
                   data: [eco4[3],eco6[3],eco8[3],eco10[3],eco12[3],eco14[3],eco16[3]]
               },{
                   name: 'own_production',
                   data: [eco4[4],eco6[4],eco8[4],eco10[4],eco12[4],eco14[4],eco16[4]]
               },{
                   name: 'own_consumption',
                   data: [eco4[5],eco6[5],eco8[5],eco10[5],eco12[5],eco14[5],eco16[5]]
               },]
           }); // Plot Energy calculation module


      } //function send_to_front



  });


</script> 

我已经尝试过click()事件,但是它不起作用。 它不绘制任何内容,当此事件不存在时,则仅在加载网页时才具有图表。 有什么想法吗?

我在调用图表( #container )之前用if来解决它。 我不知道我可以在$(function(){....});混合使用JQuery和javascript $(function(){....});

暂无
暂无

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

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