2014-09-02 37 views
1

我有一個自定義函數,我已經定義了,然後隨後在頁面加載時調用。我需要的功能,因爲它是填充一組selectlists運行該頁面加載時:在JQUERY成功上調用函數

<!---Populate Custom Service Description select lists --->   
<script type="text/javascript"> 
function PopulateSelectLists(){ 
    // Important: Must append the parameter "&returnformat=json" 
    $.ajax({ 
     url: 'cfcs/get_descriptions.cfc?method=getDescriptions&returnformat=json' 
     , dataType: 'json' 
     , success: function(response){ 
      $.each(response.DATA, function(I, row){ 
      // get value in first column ie "description" 
      var description = row[0]; 

      // append new option to list 
      $("#descriptionDD1").append($('<option/>', { 
        value: description, 
        text : description 
      })); 
      $("#descriptionDD2").append($('<option/>', { 
        value: description, 
        text : description 
      })); 
      $("#descriptionDD3").append($('<option/>', { 
        value: description, 
        text : description 
      })); 
      $("#descriptionDD4").append($('<option/>', { 
        value: description, 
        text : description 
      })); 
     }); 
     }, 
     error: function(msg){ 
      console.log(msg); 
     } 
    }) 
} 
$(document).ready(PopulateSelectLists); 
</script> 

我想打電話給我的自定義功能PopulateSelectLists()當另一個AJAX調用成功完成,所以我可以刷新我selectlists但似乎永遠不會打電話給我的功能:

<!---Script to add new description to DB ---> 
<script> 
$(function(){ 
//Add a new note to a link 
$("#addDescriptionForm").submit(function(){ 
    // prevent native form submission here 
     $.ajax({ 
      type: "POST", 
      data: $('#addDescriptionForm').serialize(), 
      url: "actionpages/add_descriptions_modal.cfm", 
      success: function() { 
       PopulateSelectLists(); //Call Function to refresh selectlist 
       $("#addDescriptionResponse").append("Successfully added description."); 

       }  
     }); 
     return false;   
    }); 
}); 
</script> 

我在哪裏錯了?

+1

控制檯中是否存在錯誤?如果您將alert或console.log添加爲第一個成功行,您甚至可以獲得成功嗎?你調試了js嗎? – 2014-09-02 13:58:19

+1

__it似乎永遠不會調用我的功能_ - 請看看控制檯,並添加一些調試console.log,即爲了驗證這 – 2014-09-02 13:59:36

+0

我刷新了我的瀏覽器,我可以看到它現在工作。這可能是我的錯誤,因爲我認爲瀏覽器緩存正在搞砸了。 – 2014-09-02 14:54:51

回答

1

在有問題的情況下,請求是否實際返回成功?用Firebug或Fiddler或任何你正在使用的開發工具欄檢查響應。可能是由於服務器錯誤,連接錯誤或任何其他可能導致請求丟失的原因,所以它不會成功。

您也可以看看使用完整處理程序:http://api.jquery.com/jquery.ajax/

+1

這不是一個答案,它是一個評論。 – 2014-09-02 14:07:53

+0

是的,但結果有點太長了評論。如果使用完整的事件適用於OP,它將成爲答案;) – rdmptn 2014-09-02 15:03:10