2016-09-24 1 views
-2

JQuery 함수에서 하이퍼 링크에서 서블릿으로 데이터를 전달하는 방법은 무엇입니까?

$(document).ready(function(){ 
 
    alert("inside function"); 
 
    $(".month").click(function(e){ 
 
     alert("hii"); 
 
     $("a").click(function(){ 
 
      var m = $(this).attr("id") // or you can use this.id 
 
      alert("current selected month"+m); 
 
      $.ajax({ 
 
        type : "POST", 
 
        url : "Controller?action=month", 
 
        data : m, 
 
        success : function(data) { 
 
         alert("sucess"); 
 
        } 
 
       }); 
 
     }); 
 
    });    
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="bg1"> 
 
    <div class="bg2"> 
 
     <div class="main"> 
 
      <div class="box"> 
 
       <article id="content"> \t \t \t 
 
        <div class="box1"> 
 
         <div class="wrapper"> 
 
          <div class="h"> 
 
           <div class="h1"> \t \t \t \t \t 
 
            <ul class="month"> \t \t \t \t \t 
 
             <li><a class="month" id="01" href="#">January</a></li> 
 
             <li><a class="month" id="02" href="#">February</a></li> 
 
             <li><a class="month" id="03" href="#">March</a></li> 
 
             <li><a class="month" id="04" href="#">April</a></li>       
 
             <li><a class="month" id="05" href="#">May</a></li> 
 
             <li><a class="month" id="06" href="#">June</a></li> 
 
             <li><a class="month" id="07" href="#">July</a></li> 
 
             <li><a class="month" id="08" href="#">August</a></li> 
 
             <li><a class="month" id="09" href="#">September</a></li> 
 
             <li><a class="month" id="10" href="#">October</a></li> 
 
             <li><a class="month" id="11"href="#">November</a></li> 
 
             <li><a class="month" id="12"href="#">December</a></li> 
 
            </ul> \t \t \t \t 
 
           </div> 
 
           <div class="h2"> 
 
            <div align="right"> 
 
             <a href="maintenance_table.tiles" class="button2">Show All</a> 
 
            </div> 
 
            <center> 
 
             <h2>Maintenance Form</h2> 
 
             <form id="mentainenceForm" action="#"method="post"> 
 

 
              <table> 
 
               <tr class="wrapper"> 
 
                <td>Date:</td> 
 
                <td><input type="text" class="input"></td> 
 
               </tr> 
 
               <tr> 
 
                <td>Electric :</td> 
 
                <td><input type="text" class="input"></td> 
 
               </tr> 
 
               <tr class="wrapper"> 
 
                <td>Water :</td> 
 
                <td><input type="text" class="input"></td> 
 
               </tr> 
 
               <tr class="wrapper"> 
 
                <td>Internet:</td> 
 
                <td><input type="text" class="input"></td> 
 
               </tr> 
 
               <tr class="wrapper"> 
 
                <td>Maintenance:</td> 
 
                <td><input type="text" class="input"></td> 
 
               </tr> 
 
               <tr> 
 
                <td></td> 
 
                <td><a href="#" class="button2">Submit</a></td> 
 
               </tr> 
 
              </table> 
 
             </form> 
 
            </center> 
 
           </div> 
 
          </div> 
 
         </div> 
 
        </div> 
 
       </article> 
 
      </div> 
 
     </div> 
 
    </div> 
 
</div>

자세한 내용은이 날을 제안 해주십시오 .so를 얻기 위해 내가 온 클릭 hyperlink.i의 달 ID를 얻을 JQuery와 fuction를의 ID를 얻기 위해 노력하고 있지만, ID를 얻기 후에 내가 내 컨트롤러에서 해당 ID를 보내려면 코드가 잘 작동하도록 코드에서 무엇을 변경할 수 있습니까?

+0

$(document).ready(function(){ alert("inside function"); $(".month").click(function(e){ e.preventDefault(); var m = $(this).attr("id") // or you can use this.id alert("current selected month"+m); $.ajax({ type : "POST", url : "Controller?action=month", data : m, success : function(data) { alert("sucess"); } }); }); }); 
: 'URL : "컨트롤러 액션 = 달"+ "& monthValue ="+ m,'얻을 귀하의 컨트롤러 자바 요청 매개 변수로 monthValuein – Pat

+0

나는 당신이 제안하지만 서블릿에 가지 않는 컨트롤로 수정했습니다. –

답변

0

먼저해야합니다 preventDefault 태그를 클릭하면 코드가 있습니다.

$(".month").click(function(e){ 
    e.preventDefault(); 
-1

jquery 라이브러리를 포함해야한다는 것을 이해하면 코드에서 Jquery를 사용하고 있습니다. 파일에이 코드 포함을 시도한 다음 실행하십시오.

src = "https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
+0

예 jquery 라이브러리를 포함 시켰습니다. –

0

내부 클릭 수신기가 필요하지 않습니다. 클래스 .month의 요소에 대한 모든 클릭 이벤트가 처리됩니다.

귀하의 수정 JS :

당신은 당신의 아약스 호출에서 URL에 달 추가 할 수 있습니다
+0

서블릿은 call.i을 사용하여 web.xml에서 controller 액션을 제공하고 request.getparameter.but에 의해 한 달이 지났음에도 불구하고 code.But를 수정했습니다. system.out.println ("inside servlet"); 콘솔에 인쇄하십시오. –

+0

앱에서 라우팅을 확인하고 ajax 요청에서 url을 확인하십시오. 여전히 도움이 필요하면 질문을 서블릿 코드로 업데이트하십시오 – ShabashP

관련 문제