2010-12-01 4 views
0

양식 내에서 <div> 몇 개가 있는데, div의 각 항목에는 특정 양식이 들어 있습니다. 사용자가 제출 버튼을 누르면 , 나는 사용자가 내가 원하는 제출 버튼이있는 div에 내용에 따라 서로 다른 쿼리를 실행할 수있는 프로그램을 누르면다른 쿼리를 실행하는 서버에 다시 게시

<form method="get" action="addprogramtodb.jsp"> 
<select name="cid" style="display: none;"> 
<option>1</option> 
<option>2</option> 
</select> 

<div id="1"> 
</div> 

<div id="2"> 
</div> 

<div id="3"> 
</div> 

<input type="submit"/> 
</form> 

에 따라 다른 동작을 실행하려면 ... . div id를 기준으로 또는 어떤 식 으로든.

+0

좀 더 애매 할 수 있습니까? – Pointy

답변

1

제출 버튼에 일반적인 방식으로 이름과 값을 지정합니다.

<input type="submit" name="action" value="action1"> 
... 
<input type="submit" name="action" value="action2"> 
... 
<input type="submit" name="action" value="action3"> 

누르는 버튼은 요청 매개 변수로도 사용할 수 있습니다.

String action = request.getParameter("action"); 

if ("action1".equals(action)) { 
    // action1 button is pressed. 
} else ("action2".equals(action)) { 
    // action2 button is pressed. 
} else ("action3".equals(action)) { 
    // action3 button is pressed. 
} 

당신은 필요하면 그들에게 다른 이름을 부여 할 수 있습니다

대신 다음 각 요청 매개 변수를 nullcheck.

<input type="submit" name="action1" value="This is more i18n friendly"> 
... 
<input type="submit" name="action2" value="Blah"> 
... 
<input type="submit" name="action3" value="More blah"> 

if (request.getParameter("action1") != null) { 
    // action1 button is pressed. 
} else (request.getParameter("action2") != null) { 
    // action2 button is pressed. 
} else (request.getParameter("action3") != null) { 
    // action3 button is pressed. 
} 

또는 실제로 모든 에서 <form> 자신의 경우는, 당신은 또한 함께 숨겨진 입력을 전달할 수 있습니다.

<form> 
    <input type="hidden" name="action" value="action1"> 
    ... 
</form> 
<form> 
    <input type="hidden" name="action" value="action2"> 
    ... 
</form> 
<form> 
    <input type="hidden" name="action" value="action3"> 
    ... 
</form> 

첫 번째 예와 동일한 서버 측 처리를 사용합니다.

+0

그래서 각 div 태그 안에 3 회의 제출을해야합니까? 모두 같은 이름인가? 동작? 괜찮다고 생각하니? – aherlambang

+0

기본 아이디어 만 제공하면됩니다. 원하는 이름을 선택할 수 있습니다. 요점은 눌려진 버튼의 이름/값이 요청 파라미터로서 또한 이용 가능하다는 것이다. 서블릿에서 일반적인 방법으로 확인합니다. – BalusC

+0

div를 양식으로 변경할 수 없습니다 ... 다른 div의 동일한 이름을 가진 3 개의 입력이 충분할지 여부를 알고 싶습니다. – aherlambang

관련 문제