我在html頁面上添加了一個按鈕來進行一些繪圖。 按鈕可以正確繪製圖形,但當向代碼添加警告框時,點擊將按預期工作。我的發佈代碼中是否有可見的錯誤?jQuery按鈕不支持單擊,但在添加到警報框時起作用
我該如何實現這個才能正常工作?
下面是部分代碼:
// append to p container
pcontainer = $('<p></p>').append(button).appendTo('#plot');
//$('<input type="button" value="Plot Data" />').click(getData).appendTo('#plot');
// add all the variables}
/*
* Download data for all the requested variables.
*/
function getData() {
var baseUrl = document.getElementById('url').value; // get the url address
var x1,x2;
var url = baseUrl + '.dods?';
for (var i=0; i < document.variablesX.vx.length; i++) {
var selectedX = $('#variablesX :radio').filter(':checked');
//define the variables X that have been selected
if (selectedX.length ==0) {
alert("please choose ONE variable foraxisX");
return;
}
var selectedY = $('#variablesY :radio').filter(':checked'); // define the variables Y that have been selected
if (selectedY.length ==0) {
alert("please choose ONE variable for axis Y"); return;
}
var selectedType = $('#myplot :radio').filter(':checked'); // define the plot type that have been selected
if (selectedType.length ==0) {
alert("please choose ONE plot type"); return;
}
if (document.variablesX.vx[i].checked) {
// for each selected variable, get the data and pass to textarea.
var url1 = url + document.variablesX.vx[i].id;
loadData(url1, function(data) {
x1 = toJsonString(data); //alert(x1);
}, '/proxy/'); // load data from url1
};
if (document.variablesY.vy[i].checked) {
// for each selected variable, get the data and pass to textarea.
var url2 = url + document.variablesY.vy[i].id;
loadData(url2, function(data) {
x2 = toJsonString(data);}, '/proxy/'); // load data from url2
};
};
//alert ("You have chosen to plot.");
plotData(x1,x2);
}
function plotData(x1,x2) {
var arr1, arr2; // define 1 dimentional array to get splited strings
var d1 = []; // define array to put the two variables together
arr1 = x1.split (",");
arr2 = x2.split (","); // convert string into array
for (var i = 0; i < arr1.length; i += 1)
d1.push([arr1[i], arr2[i]]); // combine two variables into one array
// plot in flot
$(function() {
$.plot($("#placeholder"),[d1]);
});
}
有沒有在我的代碼可見的錯誤? 你能幫我實施我描述的嗎?
您將您的點擊處理程序附加到p元素,而不是您的按鈕。這還不是解釋,但是可能是意外行爲的來源。對於更詳細的檢查,可能需要一些標記和代碼上下文。 – Thomas 2010-10-04 08:36:23
我也測試了按鈕的以下代碼,但它仍然不能用於第一次點擊//您的按鈕 button = $('')。點擊(getData), //追加到p容器 pcontainer = $('
').append(button).appendTo('#plot'); – Mengfei 2010-10-04 08:42:30plotData()在做什麼? – 2010-10-04 08:47:39