[출처] [tjjoker]JQuery ASP.NET 컨트롤 ID제어하기|작성자 쯩이삼
============================================================
참고 : * jQuery JavaScript Library v1.4.4 * http://jquery.com/
내용 : $("[id$='subject']").eq(1) 사용법 인지
$(#"아이디풀네임") 을 적어서 접근을 하지만,
id$='아이디일부명' 으로 접근이 가능하다. 단 이름이 AA 면 1_AA 등은 가능하지만 AA_ 식으로는 안된다는것이다.
즉, *AA는 되지만 AA* 는 같은 이름으로 인지를 못한다.
일단 같은이름으로 인지가 되면, jQuery은 .eq(인덱스) 를 통해 해당 컨트롤에 접근이 가능하다.
[참고] 컨트롤 존재여부 판단
$("[id$='chk_ItemId']").length 식으로 length 를 하면 없는 객체도 0으로 반환을하므로
존재 여부 판단 가능하다.
======================================================================================
<%@ Page Language="C#" %>
<script src="/jQuery/jquery.js" type="text/javascript"></script>
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>제목 없음</title> <script type="text/javascript"> // TEST일 : 2011 - 02 - 09 // 참고 URL http://cafe.naver.com/jquerymytour.cafe?iframe_url=/ArticleRead.nhn%3Farticleid=64 function test() { //id$='형식으로 적으면' 해당 리스트가 속해있는 이름들을 찾는다. //1.서버 텍스트 박스 값 가져오기 alert("1. 서버 텍스트 박스 값 >>" +$("[id$='_subject']").val()); //2. 서버 텍스트 박스 값 변경 $("[id$='_subject']").val("변경"); // 2개다 변경됨 (txt_subject1 변화없음) // _subject2 면 제외댐, 앞은 아무거나 상관없음. 뒤에가 동일한 이름인 모든것을 검색함 ,동일한이름은 배열로저장되어접근가능 ////////////////////////////////////////////////////////////////// alert("2. 서버 텍스트 박스 값 >>" +$("[id$='_1_TextBox']").val());// _1_TextBox 의값 alert("3. 서버 텍스트 박스 값- 배열 1번째값 >>" +$("[id$='_TextBox']").eq(0).val()) alert("4. 서버 텍스트 박스 값- 배열 2번째값 >>" +$("[id$='_TextBox']").eq(1).val()) ////////////////////////////////////////////////////////////////// alert("5. 서버 텍스트 박스 값- 배열 2번째값 >>" +$("[id$='subject']").eq(1).val()) //변경나옴 alert("6. 서버 텍스트 박스 값- 배열 3번째값>>" +$("[id$='subject']").eq(2).val()) //undefined 나옴 (txt_subject1은 배열로 인식이안됨) alert("7. 서버 텍스트 박스 값- 배열 0번째값 >>" +$("[id$='subject']").val() + "=" + $("[id$='subject']").eq(0).val()) var a = $("[id$='subject']"); alert("배열갯수" + a.length); // $("#<%=txt_subject.ClientID%>").val();로 작업해도 무방 } </script> </head> <body> <form id="form1" runat="server">
<asp:Button ID="Button1" OnClientClick="test()" runat = "server"/> s<br /> <asp:TextBox ID="txt_subject" runat="server" class="intstyle1" style="width:310px;" ></asp:TextBox> <asp:TextBox ID="tx2t_subject" runat="server" class="intstyle1" style="width:310px;" ></asp:TextBox> <asp:TextBox ID="txt_subject1" runat="server" class="intstyle1" style="width:310px;" Text = "아무변화없음" ></asp:TextBox> <br /> <asp:TextBox ID="_1_TextBox" runat="server" class="intstyle1" style="width:310px;" Text = "1" ></asp:TextBox> <asp:TextBox ID="_2_TextBox" runat="server" class="intstyle1" style="width:310px;" Text = "2"></asp:TextBox> </form> </body> </html>