2012-06-11 39 views
0

這裏是我的提交按鈕通過Ajax動態地寫着:的onclick調用函數從提交輸入不工作

var htmlpage = "<div class='pages'>" 
       for (i=1 ; i < pages+1 ; i++) 
        { 
         htmlpage += "<li><input type='submit' value='"+i+"' onclick='updatefilters;' /></li" 
        } 
       htmlpage += "<div>"  
       htmlpage += "</ul>"; 

我試圖重新運行updatefilters()函數來更改顯示的項目。我想在沒有看到所有代碼的情況下,它有點難以概念化......但是基本上,我需要做的就是在每次點擊提交按鈕時再次運行該函數......現在,它給了我一個updatefilters is undefined錯誤在螢火蟲。

我的繼承人整個JS參考

$(function() { 
    $("#selectable").selectable({ 
     selected: updatefilters 
    }); 
    getactivesession(); 
    function getactivesession(ev, ui){ 
     var i = 0; 
     var actfilter, strfilter; 
     var strfilterarray = new Array(); 
     $.ajaxSetup({cache: false}) 
     $.ajax({ 
     type: "POST", 
     async: false, 
     url: 'welcome/getactivesession', 
     dataType: 'json', 
     success: function (data){ 
      strfilter = JSON.stringify(data) 
      strfilterarray = strfilter.split(',') 
      for (i=0 ; i < strfilterarray.length ; i++) { 
       strfilter = strfilterarray[i] 
       strfilter = strfilter.replace(/[\[\]'"]+/g,''); 
       var strfilterdash = strfilter.replace(/\s+/g, '-') 
       actfilter = '#'+ strfilterdash 
       $(actfilter).addClass('ui-selected') 
      } 
      updatefilters(); 
     } 
     }); 
    } 

    function updatefilters(ev, ui){ 
     // get the selected filters 
     var template, html; 
     var i = 0; 
     var page; 
     if(! page){ 
      page = 0; 
     } 

     var $selected = $('#selectable').children('.ui-selected'); 
     // create a string that has each filter separated by a pipe ("|") 
     var filters = $selected.map(function(){return this.id;}).get().join("\|"); 
     $.ajax({ 
      type: "POST", 
      async: false, 
      url: 'welcome/updatefilters', 
      dataType: 'json', 
      data: { filters: filters, page: page }, 
      success: function(data){ 
       var html = ""; 
       html += "<div id=board>" 
       html += "<div class='board' id='table'>" 
       html += "<div id='row'>header here</div>" 
       var pages = Math.ceil(data['num_threads']/10); 
       var htmlpage = "<div class='pages'>" 
       for (i=1 ; i < pages+1 ; i++) 
        { 
         htmlpage += "<li><input type='submit' value='"+i+"' onclick='updatefilters;' /></li" 
        } 
       htmlpage += "<div>"  
       htmlpage += "</ul>"; 
       htmlpage += "</br>"; 
       html += htmlpage; 
       for (i=0 ; i < data['threads'].length ; i++) 
       { 
        html += "<div id=row>"; 
        html += " <div id='author' style='background: url("+data['threads'][i].location + ") no-repeat; background-position: center;'><p>"+data['threads'][i].username + "</p></div>"; 
        html += " <div id='arrow'></div>"; 
        html += " <div id='subject' title='"+ data['threads'][i].body +"'>"; 
        html += "  <a href=thread/" + data['threads'][i].slug + ">"+ data['threads'][i].subject +"</a><p>Created: "+data['threads'][i].posttime+"</p></div>";      
        html += " <div id='info'>"; 
        html += "  <div id='replies'>" + data['threads'][i].replies_num + "</div>"; 
        html += "  <div id='lastpost'>"+ data['threads'][i].lastreply+"</div>"; 
        html += " </div>"; 
        html += "</div>"; 
       } 
       html += "</div></div>"; 
       $('#board').html(html); 
      } 
     }); 
    } 
}); 

回答

1

似乎有這種方法的幾個問題。

首先,你實際上並沒有調用你的onclick處理程序中的函數。

htmlpage += "<li><input type='submit' value='"+i+"' onclick='updatefilters;' /></li" 

應該是:

htmlpage += "<li><input type='submit' value='"+i+"' onclick='updatefilters();' /></li" 

其次,updatefilters功能不是從全球範圍內,這是在匿名函數將被執行訪問。您必須將onload回調之外的function updatefilters(ev, ui)移至腳本塊的頂部。

+0

將它移出onload回調工作。謝謝! (失蹤()也沒有幫助,但我刪除它只是爲了檢查錯誤)。謝謝! –