jQuery按钮不适用于单击,但在添加到警告框时有效

时间:2010-10-04 08:29:42

标签: jquery html button event-handling onclick

我在html页面上添加了一个按钮来做一些绘图。 用于正确绘制图形的按钮,但是当我向代码添加警告框时,单击按预期工作。我发布的代码中是否有明显的错误?

如何实现此功能才能正常工作?

以下是代码的一部分:

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

我的代码中是否有明显的错误? 你能帮我实现我所说的吗?

1 个答案:

答案 0 :(得分:0)

请发布getData()例程中的代码,该例程从服务器获取数据。

假设您正在使用Ajax $ .get(或类似),这是一个异步调用,其后的语句将在获取数据的调用实际完成之前执行。在ajax调用的success函数中包含以下语句,或者将async设置为false的数据。

您的提醒正在减慢进程,以便在继续下一步之前完成获取数据。