자바스크립트에서 form을 이용해 submit을 하면
다른 페이지로 이동하는 건 다 아는 사실인데.
내가 form submit을 하지 않아도
form submit이 되는 버그가 발생했다.
내 소스는 아래와 같다.
<script> if (str == '딸기') { location.href = 'index.do'; //$(location).attr("href", "index.do"); //둘 다 동일하게 작동한다. } </script> <form id="frm" action="index2.do"></form>
만약 str이 딸기면
location.href 명령어를 사용해 index쪽으로 이동하는 데
문제는 form.submit() 명령어를 사용하지 않아도 폼 전송이 되어버린 다는 것이다.
폼의 action이 문제인가 해서
$("폼아이디").attr("action", "index.do");로 변경해도 별 소용이 없었다.
내가 발견한 해결책은
<script>
if (str == '딸기') {
$("#frm").attr("action",""); //혹시나 방지용 $("#frm").attr("onsubmit","event.preventDefault();"); //이것만 써도 된다.
location.href = 'index.do';
}
</script> <form id="frm" action="index2.do"></form>
위와 같이 onsubmit 속성에 event.preventDefault() 소스를 추가하는 것이다.
그러면 submit 이벤트가 멈추게 된다.
별짓 다해봤는데
위의 소스가 가장 정답에 가깝다.