简体   繁体   English

两个单独的函数,但是我希望它们同时被初始化

[英]Two separate Functions, but I want them to be initialized at the same time

I have these 2 separate functions, each work and are initialized by 2 different input buttons which are used to pass two different Country Names to create a html table comparison of the 2 countries from a SQL database. 我有这2个单独的函数,每个函数都由2个不同的输入按钮初始化,这些按钮用于传递两个不同的国家/地区名称,以从SQL数据库创建两个国家/地区的html表比较。

How can I rewrite this to initialize both function together with 1 button not 2? 我该如何重写此代码以使用1个按钮(而不是2个)来初始化两个功能?

    <script type="text/javascript">

        function display_results_table() {
            $("medal_table").empty();
            $('<table id = "results_table">').appendTo('#medal_table');
            $.get("sam2.php", { Country_1: $('#Country_1').val(), Country_2: $('#Country_2').val(), queryType: $('#differentOptions').val() }, 

        function (results_obtained) {

            $('<tr><td>Rank</td>' +
            '<td>Country Name</td>' +
            '<td>Gold</td>' +
            '<td>Silver</td>' +
            '<td>Bronze</td>' +
            '<td>Total</td></tr>').appendTo('#results_table');

            for (var i = 0; i <= results_obtained.length; i++) {
                $('<tr><td>' + (i+1) + '</td>' + 
                '<td>' + results_obtained[i].country_name + '</td>' +
                '<td>' + results_obtained[i].gold + '</td>' +
                '<td>' + results_obtained[i].silver + '</td>' +
                '<td>' + results_obtained[i].bronze + '</td>' +
                '<td>' + results_obtained[i].total + '</td></tr>').appendTo('#results_table');              
            }   
        },'json');

        $('</table>').appendTo('#medal_table');
        }

        function display_cyclist_results_table() {
            $("cyclist_table").empty();
            $('<table id = "cyclist_results_table">').appendTo('#cyclist_table');
            $.get("sam3.php", { Country_1: $('#Country_1').val(), Country_2: $('#Country_2').val(), queryType: $('#differentOptions').val() }, 

        function (cyclist_results_obtained) {

            $('<tr><td>Name</td></tr>').appendTo('#cyclist_results_table');

            for (var j = 0; j <= cyclist_results_obtained.length; j++) {
                $('<tr><td>' + cyclist_results_obtained[j].iso_id + '</td>' +
                '<td>' + cyclist_results_obtained[j].name + '</td></tr>').appendTo('#cyclist_results_table');
            }

        },'json');

        $('</table>').appendTo('#cyclist_table');
        }

    </script>
    <title>sam.php</title>
</head>
<body>
    <form name="form">
        <table>
            <tr><td><input name="Country_1" id="Country_1" value="GBR" type="text"></td></tr>
            <tr><td><input name="Country_2" id="Country_2" value="USA" type="text"></td></tr>
            <tr><td><input type="button" value="Medal Comparison" onClick="display_results_table()"/></td>
                <td><input type="button" value="Cyclist Comparison" onClick="display_cyclist_results_table()"/></td></tr>
        </table>
    </form>
    <div id="medal_table"></div>
    <div id="cyclist_table"></div>

</body>

Use on() or click() function on a new button (or any element you want): on()新按钮(或所需的任何元素click()上使用on()click()函数:

$('#button').click(function() {
   functionOne();
   functionTwo();
});

Wrap it into a function 包装成一个函数

function call_everybody(){
    display_results_table();
    display_cyclist_results_table();
}

So your button just need this 所以你的按钮只需要这个

<input type="button" value="Cyclist Comparison" onclick="call_everybody()"/>

Use a jQuery handler instead of inline onclick handlers: 使用jQuery处理程序,而不是内联onclick处理程序:

$('#button').click(function() {
    display_results_table();
    display_cyclist_results_table();
});

Inline handlers separate the event registration from the event handler for no good reason. 内联处理程序没有充分的理由将事件注册与事件处理程序分开。 This is easier to maintain. 这更容易维护。

This just requires a way to identify the button (eg an ID added to it): 这仅需要一种识别按钮的方法(例如,添加了ID的按钮):

<input id="button1" type="button" value="Medal Comparison" />

暂无
暂无

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

相关问题 两个不同文件中的两个异步代码。 我希望它们一个接一个地运行,但它们同时运行 - Two asynchronous codes in two different files. I want them to run one by one but they run at the same time 我想要一键根据时间在两个功能之间切换? - I want one button to switch between two functions based on time? 是否可以将两个单独的函数绑定到同一事件 - Is it possible to bind two separate functions to the same event 为两个单独的排序函数输出相同的值 - Outputting same value for two separate sorting functions 如何同时创建两个Onload函数? - How can I create two Onload functions at the same time? 我想在两个功能之间切换,这段代码只工作一次,但我想让它每次点击都工作 - i want to toggle between two functions this code works only one time but i want to make it work each time i click 我想基于相同的数据创建两个单独的变量,这些数据在排序时不会相互影响 - I want to create two separate vars based off the same data that when sorted dont affect each other 我有 6 个同时打开的弹出框,但希望它们一一打开 - I have 6 popovers that open at the same time but want them to open one by one 如何在同一段落中同时显示两个ID中的两个函数的output? - How do I display output from two functions in two IDs at the same time in the same paragraph? 如何在使两个变量名成为同一对象的别名与使它们等于两个单独的对象之间切换? - How do I switch between making two variable names be an alias of the same object to making them equal two separate objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM