javascript
  • jquery
  • ajax
  • 2013-01-07 25 views 0 likes 
    0

    我在下面通過運行getGrades函數調用此代碼。使用jQuery將數據加載到字段中AJAX

    function getGrades(grading_company) { 
        // Set file to get results from.. 
        var loadUrl = "ajax_files/get_grades.php"; 
    
        // Set data string 
        var dataString = 'gc_id=' + grading_company; 
    
        // Set the callback function to run on success 
        var callback = showGradesBox; 
    
        // Run the AJAX request 
        runAjax(loadUrl, dataString, callback); 
    } 
    
    function showGradesBox(response) { 
        // Load data into grade field 
    
        // Hide condition fields 
        jQuery('#condition').hide(); 
        jQuery('#condition_text').hide(); 
    
        // Show grade fields 
        jQuery('#grade_wrapper').show(); 
        jQuery('#grade_text_wrapper').show();  
    } 
    
    function runAjax(loadUrl, dataString, callback) { 
        jQuery.ajax({ 
         type: 'GET', 
         url: loadUrl, 
         data: dataString, 
         dataType: 'html', 
         error: ajaxError, 
         success: function(response) { 
          callback(response); 
         } 
        });  
    } 
    

    現在你可以看到我傳遞Ajax響應數據的showGradesBox功能;但是我現在不知道如何將它加載到現場。

    我見過使用​​的例子,但似乎你必須一次性使用這個URL;我遇到的唯一可能使用的唯一其他功能是.html();但它的描述聽起來不對!!

    +0

    get_grades.php返回的是什麼? – mplungjan

    回答

    1

    .html()應該工作之前,你的功能分配的功能,名爲showGradesBox變量...

    當的.html()用於設置元素的內容,那個元素中的任何內容都被新內容完全替代。另外,在用新內容替換這些元素之前,jQuery會從子元素中移除其他構造,例如數據和事件處理程序。

    function showGradesBox(response) { 
    
        // Load data into grade field 
        jQuery('#yourgradefieldID').html(response); 
    
        // Hide condition fields 
    jQuery('#condition').hide(); 
    jQuery('#condition_text').hide(); 
    
    
    // Show grade fields 
    jQuery('#grade_wrapper').show(); 
    jQuery('#grade_text_wrapper').show();  
    
    } 
    
    +0

    好酷,謝謝! – Brett

    +0

    歡呼..讓我知道如果有任何問題... – bipen

    0

    假設與ID grade_text場和一個字符串從PHP返回:

    function showGradesBox(response) { 
    
        // Load data into grade field 
    
        jQuery('#grade_text').val(response); 
    
        // Hide condition fields 
        jQuery('#condition').hide(); 
        jQuery('#condition_text').hide(); 
    
    
        // Show grade fields 
        jQuery('#grade_wrapper').show(); 
        jQuery('#grade_text_wrapper').show();  
    
    } 
    
    +0

    腳本返回html數據;特別是一個選擇列表。 – Brett

    0

    這就賦予的「未定義」到你的回調的值。

    // Set the callback function to run on success 
    var callback = showGradesBox; 
    

    嘗試這樣

    var showGradesBox = function(response) { 
        // Load data into grade field 
    
        // Hide condition fields 
        jQuery('#condition').hide(); 
        jQuery('#condition_text').hide(); 
    
        // Show grade fields 
        jQuery('#grade_wrapper').show(); 
        jQuery('#grade_text_wrapper').show();  
    } 
    
    function getGrades(grading_company) { 
        // Set file to get results from.. 
        var loadUrl = "ajax_files/get_grades.php"; 
    
        // Set data string 
        var dataString = 'gc_id=' + grading_company; 
    
        // Set the callback function to run on success 
        var callback = showGradesBox; 
    
        // Run the AJAX request 
        runAjax(loadUrl, dataString, callback); 
    } 
    
    function runAjax(loadUrl, dataString, callback) { 
        jQuery.ajax({ 
         type: 'GET', 
         url: loadUrl, 
         data: dataString, 
         dataType: 'html', 
         error: ajaxError, 
         success: function(response) { 
          callback(response); 
         } 
        });  
    } 
    
    相關問題