Web/Javascript
JavaScript 다중 폼(form)이 있는 페이지에서 특정 폼의 요소에 접근하기
bboks.net
2011. 7. 12. 17:11
접근방법
예를 들어
위와 같이 페이지가 구성되어 있을 때 form2의 writer에 값을 지정할 때는
[참고] Using JavaScript to access form objects when there are multiple forms
documents.forms["폼 이름"].elements["엘레먼트 이름"];
예를 들어
<div id="dialog" style="display: none">
<form name="form1" method="post" action="save.do">
<table>
<tr>
<td>이름</td>
<td><input type="text" name="writer"></td>
</tr>
<tr>
<td>내용</td>
<td><input type="text" name="contents"></td>
</tr>
</table>
</form>
</div>
<div id="modify" style="display: none">
<form name="form2" method="post" action="save.do">
<table>
<tr>
<td colspan="2"><input type="hidden" name="id"></td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="writer"></td>
</tr>
<tr>
<td>내용</td>
<td><input type="text" name="contents"></td>
</tr>
</table>
</form>
</div>
<form name="form1" method="post" action="save.do">
<table>
<tr>
<td>이름</td>
<td><input type="text" name="writer"></td>
</tr>
<tr>
<td>내용</td>
<td><input type="text" name="contents"></td>
</tr>
</table>
</form>
</div>
<div id="modify" style="display: none">
<form name="form2" method="post" action="save.do">
<table>
<tr>
<td colspan="2"><input type="hidden" name="id"></td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="writer"></td>
</tr>
<tr>
<td>내용</td>
<td><input type="text" name="contents"></td>
</tr>
</table>
</form>
</div>
위와 같이 페이지가 구성되어 있을 때 form2의 writer에 값을 지정할 때는
document.forms["form2"].elements["writer"].value = "내 이름"
[참고] Using JavaScript to access form objects when there are multiple forms