简体   繁体   English

通过一个按钮同时执行两个javascript函数?

[英]Executing two javascript functions at the same time from one button?

<center>
        <form id="f1">
            <textarea name="textcontent" id="styled" cols="40" rows="4"></textarea>
                <br>
            <input onclick='JavaScript:xmlhttpPost("/cgi-bin/test.py")' type="button" value="Show" class="blue"/>
                <br><br>
            <div id="qList">
                <img id="loading_image" src="ajax-loader.gif"/>
            </div>
        </form>
</center>

I want the image as shown in the code to appear when the Show button is clicked and the image to be replaced by the content returned by the python file. 我希望单击“ Show按钮时Show代码中显示的图像,并且该图像将替换为python文件返回的内容。

I know I can use this script onclick="$('#loading_image').show();" 我知道我可以在onclick="$('#loading_image').show();"使用此脚本onclick="$('#loading_image').show();" , but how can I make them execute together, so that image is replaced by some other content ? ,但是如何使它们一起执行,以便将图像替换为其他内容?

First, it is better to use addEventListener , or .on from jQuery. 首先,最好使用jQuery中的addEventListener.on

Second, all you have to do is have your handler call those two functions: 其次,您要做的就是让处理程序调用这两个函数:

function handler() {
    xmlhttpPost("/cgi-bin/test.py");
    $('#loading_image').show();
}

Then bind as follows: 然后绑定如下:

<input id="myInput" type="button" value="Show" class="blue"/>

...

$("#myInput").on("click", handler);

The best way is: 最好的方法是:

 <input type="button" onclick="manyfunctions()" value="Send" /> <script src="script.js" type="text/javascript"></script> 

And script.js should be like this: 而script.js应该是这样的:

 function manyfunctions() { somefunctions() anotherfunction() //List functions to herre } 

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

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