2015-01-29 2 views
0

POST 요청을 제출 한 후에 어떤 이유로 든 자바 스크립트가 작동을 멈춘다.아약스 응답 후 내 자바 스크립트가 작동을 멈춘다

다음과 같은 현상이 발생합니다.

enter image description here

다음으로는 경고 팝업에서 확인을 클릭하고 예상 된 결과가 나타납니다 : 나는 내 웹 페이지를로드 양식을 작성하고 클릭 '나에게 클릭'

enter image description here

내가 채우 양식을 벗어나지 만 버튼을 클릭하려고해도 아무 일도 일어나지 않습니다! 내 알림 팝업도 아닙니다.

다음은 현재 사용중인 파일입니다.

내 CSS, simple2.css :

body, html { 
     margin:0; 
     padding; 
     height:100% 
    } 

    a { 
     font-size:1.25em; 
    } 

    #content { 
     padding:25px; 
    } 

    #fade { 
     display: none; 
     position:absolute; 
     top: 0%; 
     left: 0%; 
     width: 100%; 
     height: 100%; 
     background-color: #ababab; 
     z-index: 1001; 
     -moz-opacity: 0.8; 
     opacity: .70; 
     filter: alpha(opacity=80); 
    } 

    #modal { 
     display: none; 
     position: absolute; 
     top: 45%; 
     left: 45%; 
     width: 64px; 
     height: 64px; 
     padding:30px 15px 0px; 
     border: 3px solid #ababab; 
     box-shadow:1px 1px 10px #ababab; 
     border-radius:20px; 
     background-color: white; 
     z-index: 1002; 
     text-align:center; 
     overflow: auto; 
    } 

    #results { 
     font-size:1.25em; 
     color:red 
    } 

내 HTML, simple2.html :

<!DOCTYPE html> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
      <link rel="stylesheet" type="text/css" href="simple2.css"> 
      <title>simple II</title> 
    </head> 
    <body> 
      <div id="fade"></div> 
      <div id="modal"> <img id="loader" src="images/loading.gif" /> </div> 
      <div id="results"><!-- Results are displayed here --> 
        <form method="post" name="start" target="_blank"> 
          <p>Enter thing1: <input type="text"  id="thing1" name="thing1" size="10" /></p> 
          <p>Enter thing2: <input type="text"  id="thing2" name="thing2" size="10" /></p> 
          <p>Enter thing3: <input type="text"  id="thing3" name="thing3" size="10" /></p> 
          <p>Check thing4: <input type="checkbox" id="thing4" name="thing4" value=1> 
          <input type="hidden" id="state" name="state" value="one" /></p> 
        </form> 
        <button id='clickme' name='clickme'>Click me</button> 
        <script src="simple2.js?0000000000015"></script> 
      </div> 
    </body> 
    </html> 

내 자바 스크립트, simple2.js :

function openModal() { 
      document.getElementById('modal').style.display = 'block'; 
      document.getElementById('fade').style.display = 'block'; 
    } 

    function closeModal() { 
     document.getElementById('modal').style.display = 'none'; 
     document.getElementById('fade').style.display = 'none'; 
    } 

    function loadAjax(url,data, app, epoch, state) { 
      console.log ("loadAjax urls is [" + url + "] data is [" + data + "]"); 
      // document.getElementById('results').innerHTML = ''; 
      console.log ("line 14"); 
      openModal(); 
      console.log ("line 16"); 
      var xhr = false; 
      console.log ("line 18"); 
      if (window.XMLHttpRequest) { 
        xhr = new XMLHttpRequest(); 
      } else { 
        xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      if (xhr) { 
      xhr.onreadystatechange = function() { 
       if (xhr.readyState == 4 && xhr.status == 200) { 
        closeModal(); 
        console.log ("line 28"); 
        document.getElementById("results").innerHTML = xhr.responseText; 
       } 
      } 
      console.log ("line 32"); 
      xhr.open("POST", url, true); 
      xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
      xhr.send(data); 
      console.log ("line 35"); 
     } 
    } 

    document.querySelector("#results button").addEventListener("click", function(e) { 
      e.preventDefault(); 
      var inputs = document.querySelectorAll("input"); 
      var params = inputs[0].name + "=" + inputs[0].value; 
      for(var i = 1; i < inputs.length; i++) { 
        var input  = inputs[i]; 
        var name  = input.name; 
        var value  = input.value; 

        params +=  "&" + name + "=" + value; 
      } 
      alert(params); 
      loadAjax("/cgi-bin/simple2.py", encodeURI(params)); 
    }); 

... 그리고 마침내 내 CGI 스크립트, simple2.py :

#!/usr/bin/env python 

    import cgi 
    import os 
    #import cgitb 
    import cgitb; cgitb.enable() # for troubleshooting 
    import time 
    import calendar 


    def goto_state_two(a, b, c, d): 
      print """ 
    Thank you!<br> 
    <li>thing1 is [%s] 
    <li>thing2 is [%s] 
    <li>thing3 is [%s] 
    <li>thing4 is [%s] 
    <hr> 
        <form method="post" name="start" target="_blank"> 
          <p>Enter thing5: <input type="text"  id="thing5" name="thing5" size="10" /></p> 
          <p>Enter thing6: <input type="text"  id="thing6" name="thing6" size="10" /></p> 
          <p>Enter thing7: <input type="text"  id="thing7" name="thing7" size="10" /></p> 
          <p>Check thing8: <input type="checkbox" id="thing8" name="thing8" value=1> 
          <input type="hidden" id="state" name="state" value="two" /></p> 
        </form> 
        <button id='clickme' name='clickme'>Click me</button> 
        <script src="simple2.js?%d"></script> 
    """ % (a,b,c,d,calendar.timegm(time.gmtime())) 

    def goto_done(a, b, c, d): 
      print """ 
    Thank you!<br> 
    <li>thing1 is [%s] 
    <li>thing2 is [%s] 
    <li>thing3 is [%s] 
    <li>thing4 is [%s] 
    <hr> 
        <form method="post" name="start" target="_blank"> 
          <input type="hidden" id="state" name="state" value="done" /></p> 
        </form> 
        <button id='clickme' name='clickme'>Click me</button> 
        <script src="simple2.js?%d"></script> 
    """ % (a,b,c,d,calendar.timegm(time.gmtime())) 


    form = cgi.FieldStorage() 
    state = form.getvalue("state") 

    if state == 'one' : 

      thing1 = form.getvalue("thing1", "") 
      thing2 = form.getvalue("thing2", "") 
      thing3 = form.getvalue("thing3", "") 
      thing4 = form.getvalue("thing4", "") 

      goto_state_two(thing1, thing2, thing3, thing4) 

    elif state == 'two' : 

      thing5 = form.getvalue("thing5", "") 
      thing6 = form.getvalue("thing6", "") 
      thing7 = form.getvalue("thing7", "") 
      thing8 = form.getvalue("thing8", "") 

      goto_done(thing5, thing6, thing7, thing8) 

    elif state == 'done' : 
      print """ 
    Hurray you did it!<br> 
    """ 
    else: 
      print """ 

    unknown state [%s] 
    <hr> 
    """ % (state) 
+4

[이벤트 위임] (http://davidwalsh.name/event-delegate)의 작업처럼 들립니다. 즉, 이벤트 리스너는 스크립트가로드 될 때 이미 존재하지만 AJAX 반환 후에 추가되는 * future * 요소에 바인딩되지 않은 요소에 바인딩됩니다. – Blazemonger

+0

누군가가 나를 http://davidwalsh.name/으로 가리킨 지 이틀 만에 두 번째입니다. 고마워요. –

답변

1
document.getElementById("results").addEventListener("click", function(e) { 
     if(e.target && e.target.nodeName == "BUTTON") { 
      // Move Prevent Default into condition to allow 
      // other events to bubble. 
      e.preventDefault(); 
      // This is a Button click we have captured 
      var inputs = document.querySelectorAll("input"); 
      var params = inputs[0].name + "=" + inputs[0].value; 
      for(var i = 1; i < inputs.length; i++) { 
        var input  = inputs[i]; 
        var name  = input.name; 
        var value  = input.value; 

        params +=  "&" + name + "=" + value; 
      } 
      alert(params); 
      loadAjax("/cgi-bin/simple2.py", encodeURI(params)); 
     } 
}); 
관련 문제