study/TIL🐥
[javascript] 하나의 form 안에 두개 이상의 submit이 있는 경우
서나쓰
2021. 4. 26. 14:22
728x90
<form>
<input type='text' name='name' id='name'/>
<input type='submit'>
<input type='image'>
</form>
회사의 업무를 배우다가 하나의 폼 안에 두 개의 submit 버튼이 있는 경우가 있었다.
input의 type이 imgae일 때도 submit으로 분류되므로 위 코드는 submit으로 분류가 된다.
1. 보편적인 방법
- input 태그 안에 onclick 이벤트 넣어 주기
이런 식으로 onclick 안에 정해진 form action을 이 버튼을 누를 때만 바꿔주게 하는 방법이 있다.<input type="image" src="" alt="확인" onclick="javascript: form.action='/local/ok';">
2. 내가 사용한 방법
- 새로운 함수 만들어 onclick 이벤트에 함수만 넣어주기
let theform = document.frmsub;
if(certkey != inputkey) {
return false;
} else if(inputkey.length != 6) {
return false;
} else {
theform.action='/local/ok';
}
이런 식으로 새로운 function을 만들어서 input 태그에는 onclick으로 함수만 호출해 주면 된다.
1번 방식은 get으로 보내면서 테스트 할 때, 2번 방식은 post로 보내면서 유효성 검사를 위해 사용하였다.
728x90