2014-11-13 5 views
0

제목이 약간 괴상하지만 내 문제가 있다는 것을 알고 있습니다. Eclipse/JSP로 동적 웹 프로젝트를 개발 중입니다. 나는 단순히 자바 스크립트 함수를 호출하여 팝업 창을 가져오고 "return false"로 리디렉션을 방지하려고합니다. 서블릿을 아약스와 함께 사용할 것이므로 return false로 실행을 중단해야합니다. 문제는 javascript 메서드가 가끔씩 작동하지만 때로는 메서드가 전혀 호출되지 않는 경우가 있습니다. 그리고 때로는 팝업이 나오지만 false는 반환되지 않고 페이지는 "test.jsp? filePath ="로 보내집니다. 자바 스크립트를 basic.js라는 별개의 파일에 보관합니다.일식 자바 스크립트 이상한 행동

는 JSP :

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<script src="javascript/basic.js"></script> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
    <form id="showFilePath"> 
     <input name="filePath" type="text"> 
     <input type="submit" onclick="shouldWork()"> 
    </form> 
    <p id="output"> 
</body> 
</html> 

및 자바 스크립트 방법은 간단하다 : 사전에

function shouldWork() { 
    alert("hello!"); 
    return false; 
} 

많은 감사

답변

0

input type = submit을 사용하고 있으므로 "return false;"를 사용할 수 있습니다. on 양식 제출 -

<form id="showFilePath" onSubmit="return false;"> 
      <input name="filePath" type="text"> 
      <input type="submit" onclick="shouldWork()"> 
     </form> 

아약스 용 JQuery를 사용하는 것이 좋습니다. 그런 다음 사용은 자신의 방법 양식의 기본 동작을 중지하고 전화를 preventDefault() 메서드를 사용할 수 있습니다 -

<html> 
<head> 
<script> 
     $(document).on("click", ".#mySubmit", function(e) { 
      e.preventDefault(); 
      $.ajax({ 
         url: formURL, 
         cache:'false', 
         type: "POST", 
         success:function(data, textStatus, jqXHR) { 
         console.log("success"); 
        } 
       }); 
     }); 
</script> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
    <form id="showFilePath"> 
     <input name="filePath" type="text"> 
     <input id="mySubmit" type="submit"> 
    </form> 
    <p id="output"> 
</body> 
</html> 
+0

많은 Saurabh에게 감사드립니다. onsubmit 명령은 아직 작동하지만 다른 문제가 우선합니다. 나는 그것을 위해 jquery를 사용할 것이지만, 나는 먼저 다른 파일로부터 자바 스크립트를 호출하도록 돌 보았다. – emrahozkan

1

변화

<input type="submit" onclick="shouldWork()"> 

-

<input type="submit" onclick="return shouldWork()"> 
+0

가 귀하의 경우 는 false를 돌려 회신 주셔서 감사합니다; 작동하지 않습니다 (URL이 변경됨). 사실 그것은 매우 불안정합니다. 다른 빌드는 다른 결과로 변경되지 않습니다. – emrahozkan