2010-10-04 92 views
1

我在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]); 
    }); 
} 

有沒有在我的代碼可見的錯誤? 你能幫我實施我描述的嗎?

+0

您將您的點擊處理程序附加到p元素,而不是您的按鈕。這還不是解釋,但是可能是意外行爲的來源。對於更詳細的檢查,可能需要一些標記和代碼上下文。 – Thomas 2010-10-04 08:36:23

+0

我也測試了按鈕的以下代碼,但它仍然不能用於第一次點擊//您的按鈕 button = $('')。點擊(getData), //追加到p容器 pcontainer = $('

').append(button).appendTo('#plot'); – Mengfei 2010-10-04 08:42:30

+0

plotData()在做什麼? – 2010-10-04 08:47:39

回答

0

請將您的getData()例程的代碼發佈到服務器上,以獲取數據。

假設您使用的是Ajax $ .get(或類似的),這是一個異步調用,並且在調用獲取數據的調用實際完成之前,將執行隨後的語句。將以下語句包裝到ajax調用的成功函數中,或者將async設置爲false的數據包裝。

您的警報正在放慢過程,以便在執行下一步之前完成獲取數據。

+0

嗨,感謝您的評論,但沒有警報,情節甚至不能通過兩次點擊工作,似乎沒有數據已通過,實際上並不慢,你有什麼想法?謝謝! – Mengfei 2010-10-04 13:09:25

+0

它仍然可能是由於ascync阿賈克斯呼籲。請將代碼發佈到loadData()中 – Clicktricity 2010-10-04 13:25:04

相關問題