2013-04-23 57 views
2
<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> 

我想单击Show按钮时代码中显示的图像,并将图像替换为python文件返回的内容。从一个按钮同时执行两个javascript函数?

我知道我可以使用这个脚本onclick="$('#loading_image').show();",但我怎样才能让它们一起执行,以便图像被其他一些内容所取代?

+0

呼叫调用这两个函数的功能。 – 2013-04-23 18:39:46

+0

'onclick =“$('#loading_image')。show();”'无法看不见:(一个使用突兀的javascript或使用不显眼的javascript。您同时使用两种技术。 – Ejaz 2013-04-23 18:40:13

+0

可能[onclick多个javascript函数]的副本(http://stackoverflow.com/questions/3910736/onclick-multiple-javascript-functions) – 2013-04-23 18:43:10

回答

5

首先,最好从jQuery中使用addEventListener.on

其次,所有你需要做的是有你的处理程序调用这两个函数:

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

然后绑定如下:

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

... 

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

+1对于不显眼的JavaScript – War10ck 2013-04-23 18:44:43

+0

我加了这个函数,但是当我打开html页面时,gif在点击按钮之前,图像仍然出现在按钮之前! – 2013-04-23 18:50:01

+1

@AnimeshPandey您需要将图像设置为最初隐藏在HTML中的样式('visibility:hidden'或'display:none'),否则它会显示出来。 – 2013-04-23 18:52:11

-1

最好的办法是:

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

而且的script.js应该是这样的:

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

相关问题