2012-02-28 2 views
0

왜 첫 번째 경고가 정의되지 않았습니까? 알림 메시지가 나타나고 텍스트 상자는 알림을 닫을 때만 나타납니다.첫 번째 부분보기 jquery selector.attrib ('id')가 정의되지 않았습니다.

enter image description here

이후 "새"를 클릭 경고 "의 RowId". 경고를 무시하고 나면 두 개의 상자가 보입니다. enter image description here

다음은 InserterAfter 인 Ajax.Action 링크가있는보기입니다.

@Ajax.ActionLink("New", "_New", new { Controller = "Test" } 
    , new AjaxOptions() 
     { InsertionMode = InsertionMode.InsertAfter, UpdateTargetId = "divNew" }) 

<div id="divNew" /> 

여기 내 부분보기입니다. 보통 오래된 jQuery를

<!-- TODO: This script should be moved to a separate javascript file 
    because you should never mix markup and javascript. But for the 
    purpose of this demonstration I have left it in the view 
--> 
<script type="text/javascript"> 
    var onSuccess = function (result) { 
     $('#divNew').append(result); 
    }; 
</script> 

@Ajax.ActionLink(
    "New", 
    "_New", 
    new { controller = "Test" }, 
    new AjaxOptions() { OnSuccess = "onSuccess" } 
) 
<div id="divNew" /> 

또는 멀리 Ajax.* 도우미를 던져 및 사용 :

RowId: @Html.TextBox("RowId")<br /> 
<script language="javascript"> 
    $(document).ready(function() 
    { 
     alert($('input[name="RowId"]').attr('Id')); 
    }); 
</script> 

답변

2

당신의 주 화면에서 다음을 시도

<!-- TODO: This script should be moved to a separate javascript file 
    because you should never mix markup and javascript. But for the 
    purpose of this demonstration I have left it in the view 
--> 
<script type="text/javascript"> 
    $(function() { 
     $('#mylink').click(function (result) { 
      $.ajax(this.href, { 
       success: function (result) { 
        $('#divNew').append(result);   
       } 
      }); 
      return false; 
     }); 
    }); 
</script> 

@Html.ActionLink(
    "New", 
    "_New", 
    new { 
     controller = "Test" 
    }, 
    new { 
     id = "mylink" 
    } 
) 
<div id="divNew" /> 

이 말했다되고, 내가 경우 오해하지 마시고 내 기억이 좋다면 이전에 비슷한 질문을 한 적이 있다고 생각합니다. 그러나 여기에서 반복하겠습니다. 그래서 다른 사람들도 그것을 볼 수 있습니다 :

하지는 어떠한 부분 또는 뷰의 모든 자바 스크립트를 넣어 않습니다. Javascript는 별도의 파일에 속하므로 마크 업과 섞어서는 안됩니다.

나는이 실수를 반복하는 사람들을 계속보고 있으며, 나는 그것을 지적하는 것이 중요하다고 생각한다.

P .: 아마도 .attr('Id') 대신 .attr('id')을 사용하려고했을 것입니다.

+1

+1에 대한 조언 : –

+0

감사합니다. 나는 모든 js를 분리 할 것이다! –

+0

내가 제안하는대로 jquery ajax와 함께 갈 것입니다. .js 파일을보기에 대한 코드와 같이 사용할 계획입니다. –

관련 문제