2014-04-23 3 views
0

jquery ajax 호출 후 내 jquery 함수가 작동하지 않습니다. 여기jquery ajax 게시 후 작동하지 않습니다. asp.net mvc4

<input type="button" value="Comment" class="submitComment" onclick="AddItem(this);" /> 

<script type="text/javascript"> 
    function AddItem(data) {   
      postData = $(data).parents().find("textarea"); 
      var input = { "PrdHierKey": postData.parents().find('input[data-attr="ProdKey"]').val(), 
       "GeoHierKey": postData.parents().find('input[data-attr="GeoKey"]').val(), 
       "Period": postData.parents().find('input[data-attr="Period"]').val(), 
       "Comment": postData.val() 
       }; 
      $.ajax({ 
       url: '@Url.Action("AddComments", "Card")', 
       type: "POST", 
       data: JSON.stringify(input), 
       cache: false, 
       contentType: 'application/json; charset=utf-8', 
       success: function (result) { 
        $(".commentContainer").html(result); 
       } 
      }); 


     } 
</script> 

이 아약스 호출 후 작동하지 않는 JQuery와 기능은 여기가

$(document).ready(function() { 
    $(".inputcomment").focus(function() {   
      $(this).css("height", "50px"); 
      if ($(this).val() == "Add a comment") { 
       $(this).val(""); 
      } 
      $(".submitComment").show(); 
      $(this).addClass("inputActive"); 
     }); 
}); 
+1

[이벤트 위임 (http://learn.jquery.com/events/event-delegation/), "$ ('사용합니다. commentContainer와

시도 focus (function.) {' – Satpal

+1

이 사이트의 검색 기능에는 어떤 문제가 있습니까? –

+0

가능한가요? [동적으로 생성 된 요소에 대한 이벤트 바인딩?] (http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – Satpal

답변

1

위임 이벤트를 사용하려고 내 코드

Ajax 호출이다 , DOM로드 후 ajax 호출을 통해 html로 전송됩니다 :

$(document).on('focus','.inputcomment',function() {   
      $(this).css("height", "50px"); 
      if ($(this).val() == "Add a comment") { 
       $(this).val(""); 
      } 
      $(".submitComment").show(); 
      $(this).addClass("inputActive"); 
     }); 

또는이 작업을 수행 할 수 있습니다

$('.commentContainer').on('focus','.inputcomment',function() {   
       $(this).css("height", "50px"); 
       if ($(this).val() == "Add a comment") { 
        $(this).val(""); 
       } 
       $(".submitComment").show(); 
       $(this).addClass("inputActive"); 
      }); 
1

당신의 요소는 DOM에 동적으로 생성하기 때문에, 이벤트가이 요소를 사용할 수 없습니다. 이 경우 이벤트 위임을 통해 해당 이벤트를 첨부 할 수 있습니다.

$(".commentContainer").on('focus','.inputcomment',function() { 

또는

$(document).on('focus','.inputcomment',function() { 
관련 문제