2013-07-22 3 views
0

form id을 views.py 페이지에 전달할 수 없습니다. jQuery는 'id'를보기 페이지로 전달하지 않습니다. 친절하게 말해서 내가 뭘 잘못하고 있는지 말해.jquery에서 양식 ID의 'id'를 가져 오는 방법

여기 내 jQuery입니다. 내가 if request.is_ajax() 조건을 제거하면 다음

$(function(){ 
     $('.chatroom').submit(function() { 

      $.ajax({ 
       type: "POST", 

       url: "/dashboard", 
       data : { 
          'chatroom_id' : $(this).attr('id') 
         }, 

        }); 

      }); 

이 Views.py

if request.is_ajax(): 
    if request.method == "POST": 
     chatroom_id = request.POST['chatroom_id'] 
else: 
    chatroom_id ='' 

print chatroom_id 

내 템플릿

{% for key, values in chat_data.items %} 
     <div class="container-fluid" alt = {{key}}> 
      <div class="row-fluid"> 
       <div class="span2"> 
       {{values.from}} <br/> {{values.init_query}} 
       </div> 

      <div class="span10 well"> 

       {% for k in values.chat %} 

         <label> Text : {{k.text}} </label> 
         <label> {{k.date_time}} </label> 

       {% endfor %}   

      <form action = "#" method = "POST" id = {{key}} class="chatroom"> 
       {% csrf_token %} 
        {{ form.as_p }} 

       <input type="submit" value = "Sent" class="btn btn-primary"> 
      </form> 

       </div> 
      </div> 
     </div> 
    {% endfor %} 

입니다, 그것은 다음 "Key 'chatroom_id' not found in <QueryDict: {u'reply': [u''], u'csrfmiddlewaretoken': [u'yIJct9O7WfyPnWmDosW9N5TEklRwoIHP']}>"

+0

는 제거해야 마지막','은'data' 닫는 브라켓 –

+0

당신은 페이지의 소스를 확인있었습니다? '{{key}} '값을 얻고 있습니까? –

+0

아니요. –

답변

0

가 아니라 말하는 오류를 보여줍니다 문제가 발생하지만 JS 버그가 발생합니다 (구문 오류) :

$(function(){ 
    $('.chatroom').submit(function() { 
     $.ajax({ 
      type: "POST", 
      url: "/dashboard", 
      data : { 
       chatroom_id: $(this).attr('id') 
      } // you had a dangling comma here 
     }); 
    }); // you were missing a closing bracket and paren here 
}); 
+0

이전에 오타가있는 것으로 나타 났지만 여전히 그 결과를 가져올 수 없습니다. 모든 제안은 –

1

당신은 당신이 실제로 당신이 전혀 아약스 게시물을보고하지 않는 것을 보여줍니다 is_ajax() 방법,하지만 표준 브라우저 양식 제출을 제거해야 말한다. 이것은 jQuery 코드의 기본 제출 조치를 막지 않았기 때문입니다.

이 있어야한다 :

$('.chatroom').submit(function(event) { 
    event.preventDefault(); 
    $.ajax({ 
     ... 
+0

하시기 바랍니다. 여전히'is_ajax() = True'를 얻지 못했습니다. 어떤 아이디어가 ??? –

관련 문제