2010-03-24 8 views
1

이것은 내 자바 스크립트입니다. 데이터베이스에 추가 할 때 레코드를 표시하고 새 레코드를 표시하려고합니다.자바 스크립트 결과를 표시 할 때

showrecords(); 데이터베이스에 레코드를 표시합니다. 여기서 abouts는 올바르게 작동하는 코드에 넣을 수 있습니다.

$(document).ready(function() 
{ 
    //showrecords() 

    function showrecords() 
    { 
     $.ajax({ 
      type: "POST", 
      url: "demo_show.php", 
      cache: false, 
      success: function(html){ 

       $("#display").after(html); 

       document.getElementById('content').value=''; 
       $("#flash").hide(); 

      } 

     }); 
    } 

    $(".comment_button").click(function() { 

     var element = $(this); 
     var test = $("#content").val(); 
     var dataString = 'content='+ test; 

     if(test=='') 
     { 
      alert("Please Enter Some Text"); 

     } 
     else 
     { 
      $("#flash").show(); 
      $("#flash").fadeIn(400) 
       .html('<img src="http://tiggin.com/ajax-loader.gif" align="absmiddle">&nbsp;<span class="loading">Loading Comment...</span>'); 


      $.ajax({ 
       type: "POST", 
       url: "demo_insert.php", 
       data: dataString, 
       cache: false, 
       success: function(html){ 


        // $("#display").after(html); 
        document.getElementById('content').value=''; 
        $("#flash").hide(); 

        //Function for showing records 
        //showrecords(); 

       } 
      }); 
     } 

    return false; 
    }); 
}); 
+0

코드 분할 경로는 어디에 있습니까? 이것은 다소 모호합니다 ... –

답변

0

글로벌 네임 스페이스를 오염시키는 것은 권장되지 않지만. 여기에 귀하의 코드에 대해 권하는 내용이 있습니다. 문서 준비 함수에서 showRecords()를 이동하고 업데이트 아약스 코드를 다른 함수 'updateRecords()'로 리팩터링하십시오. 문서 준비 기능 내에 이벤트 바인딩 만 있습니다.

POST 'demo_insert.php'서비스에 대한 응답으로 전체 의견을 반환하고 업데이트 서비스 성공 콜백에서 'showRecords()'를 호출 할 수 있습니다.

+0

이 경우 이벤트 바인딩은 무엇입니까? 당신은 내가 자바 스크립트 초보자의 abit 오전 abit 더 많은 것을 설명 할 수 있습니까 Thankyou 피트 – Pete

0

필자는 작업을 완료해야하는 (테스트되지 않은) 코드를 붙여 넣었습니다. 당신이 접근 할 수있는 영역에서 정의해야하는 함수를 호출하기 위해, 아래에서했던 것처럼 전역 적 (네임 스페이스에서 호출 할 수 있음) 네임 스페이스에 있는지 아니면 다른 객체의 일부로든.

전화를 걸기 전에 함수가 정의되어 있는지 확인해야합니다. 모든 것이 위에서 아래로 작동하기 때문입니다.

function showrecords() { 
    $.ajax({ 
    type: "POST", 
    url: "demo_show.php", 
    cache: false, 
    success: function (html) { 

     $("#display").after(html); 

     $('content').val(''); 
     $("#flash").hide(); 

    } 

    }); 
} 
function addComment() { 
    var test = $("#content").val(); 
    var dataString = 'content=' + test; 

    if (test == '') { 
    alert("Please Enter Some Text"); 
    } 
    else { 
    $("#flash").show(); 
    $("#flash").fadeIn(400) 
       .html('<img src="http://tiggin.com/ajax-loader.gif" align="absmiddle">&nbsp;<span class="loading">Loading Comment...</span>'); 

    $.ajax({ 
     type: "POST", 
     url: "demo_insert.php", 
     data: dataString, 
     cache: false, 
     success: function (html) { 
     //$("#display").after(html); 
     $('content').val(''); 
     $("#flash").hide(); 
     //Function for showing records 
     showrecords(); 
     } 
    }); 
    } 
} 

$(document).ready(function() { 
    showrecords() 

    $(".comment_button").click(function() { 
    addComment(); 
    return false; 
    }); 
}); 
관련 문제