[英]Calling a ajax function on page load and on click of button
I have a javascript file containing a ajax function with parameter x. 我有一个包含参数x的ajax函数的javascript文件。 I cannot reveal the function.I want to call the function on page load and on a button click.Also there is a for loop on the function for displaying the variable 10 times ie,
for(i=0;i<10;i++)
. 我无法显示该函数。我想在页面加载和单击按钮时调用该函数。该函数上还有一个for循环,用于显示变量10次,即
for(i=0;i<10;i++)
。
The code for that button is : 该按钮的代码是:
HTML file HTML文件
<html>
<title>Call web service</title>
<head>
<script type="text/Javascript" src="jquery-1.9.1.js"></script>
<script type = "text/javascript" src="ajax.js"></script>
<script type="text/javascript">
window.onload=function()
{
Webservice(1)('onload'); //call 1 where webservice is ajax function
}
</script>
</head>
<body>
<input id="button" type = "button" name = "button" value = "Call Webservice" onClick = "Webservice(5)"/>
</body>
</html>
Right now what I am getting is the onload function is getting overridden when I click the button whereas I want the outputs of both the functions to be displayed simultaneously So please help me. 现在,当我单击按钮时,我得到的是onload函数被覆盖,而我希望同时显示两个函数的输出,所以请帮助我。
Thanks in advance. 提前致谢。
Try doing this: 尝试这样做:
function f1() {
//what you need to do
}
function f2() {
//Do the same thing here also
}
This being your JS, in onload, call f1()
and for button click call f2()
. 这是您的JS,在onload中,调用
f1()
,对于按钮单击,调用f2()
。 If you want f1()
to execute before f2()
, i suggest you look into locks here: 如果您希望
f1()
在f2()
之前执行,我建议您在这里研究锁:
Your javaScript code should be something like this: 您的javaScript代码应如下所示:
var fn = function fn(x) {
// Do something
}
$(function() {
var someValue;
// someValue = ...
// Call function once document is loaded
fn(someValue);
// Bind function as a callback to button click
$('#button').on('click', function(event) {
fn(someValue);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.