我有以下功能:
// NATION
var x, y, z;
function flag(nation,area)
{
x = nation;
this.nation=nation;
var el=document.getElementById("desc");
el.innerHTML='The region you have selected is <b>'+area+'</b>';
document.getElementById("flag").innerHTML='<img src="images/flags/'+nation+'.jpg">';
}
// SERVICE
function output(service)
{
y = service;
this.service=service;
var el=document.getElementById("service-desc");
el.innerHTML='You have selected a <b>'+service+'</b> service.';
document.getElementById("clock").innerHTML='<img src="images/clock-'+service+'.png">';
}
一旦用户设置了变量: nation &amp; service 已设置然后我想运行此功能:
function selectmodel()
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Your browser does not support XMLHTTP!");
return;
}
var location=x;
var time=y;
var weight=z;
var url="selectmodel.php";
url=url+'?location='+location+'&time='+time+'&weight='+weight;
xmlhttp.onreadystatechange=stateChanged1;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
window.location.hash="slider";
}
function stateChanged1()
{
if (xmlhttp.readyState==4)
{
document.getElementById("result").innerHTML=xmlhttp.responseText;
}
}
请有人能够知道如何做到这一点吗?
谢谢, 乙
编辑 - 抱歉忘了说变量来自onclick(onClick =“flag('scotislands','苏格兰群岛');”)
答案 0 :(得分:0)
我猜你正在寻找 - 真正的Javascript精神 - 对于某种被触发的事件,也许甚至还有一种方法,但在这种情况下我只是从每个函数调用一个检查:
var x = null;
var y = null;
var ...
function trySelectModel() {
if (x == null) || (y == null) return;
selectModel(); // Your function
}
function flag(...) {
// Your existing code
trySelectModel();
}
function output(service) {
// Your existing code
trySelectModel();
}
function selectmodel() {
// Your existing code
}