简体   繁体   English

禁用先前按钮点击时的点击事件操作

[英]Disable click event actions on prior button clicks

I have a series of three buttons each with its own id. 我有一系列三个按钮,每个按钮都有自己的ID。 The buttons each fire a bs modal (the same modal - #mymodal). 每个按钮触发一个bs模态(相同的模态-#mymodal)。 The modal needs to display a google chart the key requirement being that the chart displayed in the modal must be dependent on the button clicked. 模态需要显示一个Google图表,关键要求是模态中显示的图表必须取决于所单击的按钮。 So the code structure after the google chart API on the modal is: 因此,模态上的Google Chart API之后的代码结构为:

    //show ONLY chart 1 if button 1 is clicked
    $(document).ready(function(){
        $("#btn0").click(function(){
        //add chart1 data and options
        //view chart1 in the modal

       });
   });

     //show ONLY chart 2 if button 2 is clicked
     $(document).ready(function(){
      $("#btn1").click(function(){
     //add chart2 data and options
     //view  chart2 in the modal
        });
    });


    //show ONLY chart 3 if button 3 is clicked
    $(document).ready(function(){
    $("#btn2").click(function(){
    //add chart3 data and options
    //view chart3 in the modal
     });
    });

All works fine first time around. 一切都很好第一次。 IE click the first button (#btn0) and the modal fires with ONLY chart 1 included. IE单击第一个按钮(#btn0),仅包含图表1的模式触发。

However when I click button 2 (#btn1) the modal fires again but contains chart 2 AND unwanted chart 1. 但是,当我单击按钮2(#btn1)时,模式再次触发,但包含图表2和不需要的图表1。

When I click button 3 (#btn2) the modal fires again but contains unwanted charts 1,2, as well as wanted chart 3. 当我单击按钮3(#btn2)时,模态再次触发,但包含不需要的图表1,2和所需的图表3。

Now any button I fire contains all three charts unless I refresh the page. 现在,除非刷新页面,否则我触发的任何按钮都包含所有三个图表。 Then the process repeats itself 然后该过程重复

I have tried inserting .unbind and .off methods: 我尝试插入.unbind和.off方法:

$("#btn0").unbind("click");
$("#btn0").off("click");

after the view chart1 procedure for btn0. 在btn0的view chart1过程之后。 I also tried e.stopPropagation() in the same position. 我也在相同位置尝试了e.stopPropagation()。 No luck. 没运气。 Same behaviour. 相同的行为。 But maybe I am not using these methods properly 但是也许我没有正确使用这些方法

I would appreciate any help. 我将不胜感激任何帮助。 This would seem to be a case of just getting rid of the actions created by early button-clicks prior to clicking another button. 似乎只是摆脱了在单击另一个按钮之前先单击按钮而创建的动作的情况。 However my 2 hours of efforts I'm afraid I have failed to achieve that. 但是,我经过2个小时的努力,恐怕未能达到目标。 Thanks in advance 提前致谢

AS REQUESTED Here is the modal caller. 根据要求这是模式调用者。 Just includes the graph.php - page which loads in accordance with problem reported. 仅包括graph.php-根据报告的问题加载的页面。

Get Report 取得报告

        <!-- Modal content-->
                <div class="modal-content">
                        <div class="modal-header">
                          <button type="button" class="close" data-dismiss="modal">&times;</button>
                          <h4 class="modal-title">Job Log Report - Global</h4>
                        </div>              
                        <div class="modal-body">    
                            <div class="row" style="background-color

                                    <div class="col-sm-8 text-align:center">
                                        <p style="color:darkred">Job Log Graph<br></p>                                                                                  
                                        <?php include "graphs.php"?>
                                    </div>                                      
                        </div>

                        <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close Report</button>
                        </div>
                </div>    
        </div>
    </div>

And here is the graphs.php page included in the modal above. 这是上面模态中包含的graphs.php页面。

    <!--Load the AJAX API-->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

  <script type="text/javascript"> 

  google.charts.setOnLoadCallback(drawChart);
  function drawChart() {          

        $(document).ready(function(){
            $("#btn0").click(function(){
                        // Create the data table col headings or pie segment headings
                        var data1 = new google.visualization.DataTable();
                        data1.addColumn('string', 'abc');
                        data1.addColumn('number', 'Number of Jobs');
                        data1.addRows
                        ([                            
                            DATA
                        ]);

                    // Set chart options
                        var options1 = {'title':'Global Jobs - By Status',
                                       'width':700,
                                       'height':300};

                    // Instantiate and draw our chart, passing in some options.
                        var chart1 = new google.visualization.BarChart(document.getElementById('chart1_div'));
                        chart1.draw(data1, options1);
                    });
                });

        $(document).ready(function(){
            //On button click, load new data
            $("#btn1").click(function(){
                        // Create the data table col headings or pie segment headings
                        var data2 = new google.visualization.DataTable();
                        data2.addColumn('string', '');
                        data2.addColumn('number', 'Number of Jobs by Category');                
                        data2.addRows
                        ([

                        DATA

                        ]);

                        // Set chart options
                        var options2 = {'title':'Approved Jobs - By Category-Last 12 Months',
                                       'width':700,
                                       'height':600};

                        // Instantiate and draw our chart, passing in some options.
                        var chart2 = new google.visualization.BarChart(document.getElementById('chart2_div'));
                        chart2.draw(data2, options2);
            });
        });

        $(document).ready(function(){
            //On button click, load new data
            $("#btn2").click(function(){    

                        // Create the data table col headings or pie segment headings
                                var data3 = new google.visualization.DataTable();
                                data3.addColumn('string', 'nothing doing');
                                data3.addColumn('number', 'Number of Jobs by Category');
                                data3.addRows
                        ([          
                        DATA         
                        ]);

                    // Set chart options
                        var options3 = {'title':'Approved Jobs - By Category-Last 12 Months',
                                       'width':700,
                                       'height':600};
                        // Instantiate and draw our chart, passing in some options.
                        var chart3 = new google.visualization.PieChart(document.getElementById('chart3_div'));
                        chart3.draw(data3, options3);   
            });
        });     

} }

Charts inside three divs that follow the chart script 遵循图表脚本的三个div中的图表

   <div id="chart1_div"></div>
   <div id="chart2_div"></div>
   <div id="chart3_div"></div>

I suspect it's an issue with replacing or clearing your data in the modal the second/third time around. 我怀疑这是第二次/第三次替换或清除模式中的数据时出现的问题。

Are you targeting an area in the modal and putting the chart data in with $().html()? 您是否要定位模态中的区域并使用$()。html()放置图表数据? That's probably the easiest way to ensure previous content gets overwritten. 这可能是确保覆盖先前内容的最简单方法。

Proving a sample of your "add chart1 data and options" code might help here if what I said above doesn't solve the issue. 如果我上面所说的不能解决问题,那么证明“添加chart1数据和选项”代码的示例可能会有所帮助。

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

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