2009-09-09 3 views
0

안녕하세요,JQuery와 - 포스트

가 나는 부분보기를 사용하여 만든 HTML을 반환하는 ASP.NET MVC 컨트롤러 메서드를 호출하기위한 jQuery 코드를 다음과 같은 한을 만든 요소를 ​​얻는 방법에 대해 설명합니다.

$("form[action$='ShowProperties']").submit(function() { 
$.post($(this).attr("action"), $(this).serialize(), function(response) { 

     $(this).children("div.serviceproperties").html(response); 
     // $("div.serviceproperties").html(response); 

    }); 
    return false; 
}); 

HTML 렌더링은 다음과 같다 :

<ul> 
<li class="serviceactionitem"> 
    <form method="post" action="/Service/ShowProperties"> 
     <input type="submit" value="Show General" name="[Show]"/> 
     <input id="id" type="hidden" value="1" name="id"/> 
     <input id="serviceitem" type="hidden" value="general" name="serviceitem"/> 
     <div class="serviceproperties"> </div> 
    </form> 
</li> 
<li class="serviceactionitem"> 
    <form method="post" action="/Service/ShowProperties"> 
     <input type="submit" value="Show Specific" name="[Show]"/> 
     <input id="id" type="hidden" value="1" name="id"/> 
     <input id="serviceitem" type="hidden" value="specific" name="serviceitem"/> 
     <div class="serviceproperties"> </div> 
    </form> 
</li> 

반응 양식 소자의 내부 DIV 소자의 내부에 설정해야 HTML이다. 페이지에 여러 폼 요소가 있고 적절한 양식 안에 div를 업데이트하는 방법을 모르겠습니다. 주석 처리 된 코드는 물론 페이지의 모든 양식에서 div를 업데이트합니다.

중요한 것은 $ (this)가 양식 요소를 반환하지 않는다는 것입니다. 이는 정상적인 것입니다. 그래서 응답을 처리하는 함수 내부의 적절한 양식 요소를 찾아 내고 내부의 div를 찾아 변경하는 방법이 필요합니다.

감사

답변

1

단순히 형태로 다시 선택 :

$("form[action$='ShowProperties'] div.serviceproperties").html(response); 

을 또는 첫번째 요소 캡처 : 나를 위해 그렇게 간단하지 않았다, 분명히

$("form[action$='ShowProperties']").submit(function() { 
var $form = $(this); 

$.post($(this).attr("action"), $(this).serialize(), function(response) { 

     $("div.serviceproperties",$form).html(response); 

    }); 
    return false; 
}); 
+0

을 :) 첫 번째 옵션이 작동하지 않습니다 왜냐하면 나는 쿼리를 만족시키는 요소가 더 많기 때문이다. 그러나 두 번째는 매력적인 것으로 보인다. 고마워 필립 –