我有一个看起来像这样的 .js 文件:
function regionmap () {
var width = 960,
height = 500,
centered;
var projection = d3.geo.albersUsa()
.scale(width)
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.attr("id","svg");
var states = svg.append("g")
.attr("id", "states")
d3.json("readme.json", function(json) {
d3.select("svg").selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.on("click", clicked)
.append("title")
.text(function(d) { return d.properties.name; });
});
listofnames = new Array();
listofnames.push("Regions:");
function clicked (d) {
var regionname = d.properties.name;
var currentclass = d3.select(this).attr("id") ;
if (currentclass == "active") {
d3.select(this).attr("id", "nonactive");
} else {
d3.select(this).attr("id", "active");
}
var contains;
var index;
for (var i = 0; i < listofnames.length; i++) {
if (regionname != listofnames[i]) {
contains = false;
} else {
contains = true;
index = i;
break;
}
}
if (contains == false){
listofnames.push(regionname);
} else if(contains == true){
listofnames.splice(index,1);
}
var x=document.getElementById("demo");
x.innerHTML=listofnames;
}
function sendingvariable (){
window.location.href = "../php/fileregions.php?name=" + listofnames;
}
}
问题是,当从 html 调用函数时,我首先调用函数 regionmap on click ( onclick="regionmap()) 效果很好。但是,我需要从 html 调用函数发送变量,我不能。有什么办法可以解决这个问题?