0

I have a jQuery autocomplete that runs some AJAX code when an item is selected. How can I simulate the selection of an item from the list? I want this code to run when the page is first loaded, so I will place the code inside the DOM ready function. I guess that I will have to pass some data to the method, but not sure how to do that. Thanks.

Here is the code that I want to run when the page is loaded:

$("#myAutocomplete").result(function(event, data, formatted) {
if (data){
    $.ajax({ 
        url: sURL + "utility/ajaxmuniChart1c",
        type: "POST",
        data: {muni: data[0]},
        dataType: 'json',               
        success: function(json){
            if  (data) {                    
                myWidth =  (document.getElementById('flot_widget').offsetWidth-15)+"px";
                myHeight = (document.getElementById('flot_widget').offsetWidth*.66)+"px";
                document.getElementById('placeholder').style.width = myWidth;
                document.getElementById('placeholder').style.height = myHeight;                 
                document.getElementById('container').style.display = 'block';                   
                var options = {
                    xaxis: {
                        tickDecimals: 0
                    },
                    series: {
                        lines: { show: true, fill: false, fillColor: "rgba(255, 255, 255, 0.8)" },
                        points: { show: true, fill: true }
                    }
                }; 
                if (document.getElementById('c3').childElementCount > 0){
                    document.getElementById('c3').innerHTML = "";
                };                                      
                var plotArea = $.plot("#placeholder", [json], options);                 
                var ctx = plotArea.getCanvas();                                 
                loc = sURL + 'php/saveme.php';
                var cs = new CanvasSaver(loc);                  
                var btnDownload = cs.generateButton('Download', ctx, 'PTS_Chart');                  
                c3.appendChild(btnDownload);                    
            }
        }
    })
}
})
4

1 回答 1

0

通过直接从内部单独运行 AJAX 调用解决了这个问题,$(document).ready(function()所以现在我的代码如下:

$(document).ready(function() {  
$.ajax({ 
    url: sURL + "utility/ajaxmuniChart1c",
    type: "POST",
    data: {muni: "Toronto"},
    dataType: 'json',               
    success: function(json){
    ...etc. 
于 2013-10-29T05:43:22.803 回答