2014-12-03 4 views
-1

사용자 입력을 요구하는 양식을 생성하고 새로 고치지 않고 php 파일로 보내려면 버튼을 사용하고 싶습니다. 그러나 양식을 html 파일에 첨부하고 제출하려고하면 java 기능이 작동하지 않습니다. 운영.jQuery로 양식 추가하기

제가 형태 추가하는 데 사용되는 함수 :

function reporterror(){ 
if (document.getElementById("report")){ 
    console.log("Still have report"); 
}else{ 
    $("<form id='report' action='report.php' method='post'><div ><h2 style='text-align:center;'>Report Error</h2><h3>Description:</h3><textarea rows='10' cols='60' name='report' id='description' placeholder='No more than 500 words' style='border-radius:10px;border:1px solid #000;'></textarea><input type='submit' style='border-radius:5px;'></input><input type='button' onclick='reportclose()' value='Close' style='border-radius:5px;'></input></div></form>").appendTo("#content"); 
} 

을}; 실행 didnt한다

기능 : http://cs20p.site11.com/

+0

당신은 onsubmit 이벤트를 위임 할 필요가 있습니다 : http://learn.jquery.com/events/event-delegation/ 그러나 어떻게'reporterror()'메서드를 호출합니까 ??? –

답변

0

시도 $(document).on("submit", ".report", function(event){

이 줄을 $("#report").on("submit",function(event){

을 변경 :

$(document).ready(function() { 
$("#report").on("submit",function(event){ 
    event.preventDefault(); 
    if (document.getElementById("description").value==""){ 
     $(".warning").remove(); 
     $("#description").after("<p class='warning' style='color:red'>Description is required</p>"); 
     console.log("yes"); 
    }else{ 

     $.ajax({ 
       type:"POST", 
       url:"report.php", 
       data:$("#report").serialize(), 
       success:function(){ 
        console.log("Great"); 
        document.getElementById("report").innerHTML="<h2>Thank You for helping us to improve!</h2><br><p>Your message have been successfully sented to this awesome website!</p><input type='button' onclick='reportclose()' value='Close' style='border-radius:5px;'></input>"; 
       } 
     }) 

    } 
}); 

은})

이는 지금처럼 무엇인가

및 이 $("<form id='report' ~ $("<form class='report' id='report'

클래스 대신 ID를 사용했기 때문에 문제가 표시됩니다. id 요소는 하나 일 수 있습니다

+0

클래스 대신 id를 사용했기 때문에 문제가 나타납니다. Id 요소는 하나 일 수 있습니다. – Oleg