리자

jQuery SELECT 태그 관련 내용

 
 
내용 좋네요 ^^y
[이 게시물은 관리자님에 의해 2011-10-31 16:55:28 jQuery에서 이동 됨]

댓글 1개

[펌]

// 뒤쪽에 옵션 추가

$("#myselect").append("<option value='1'>Apples</option>");
$("#myselect").append("<option value='2'>After Apples</option>");

// 앞쪽에 옵션 삽입
$("#myselect").prepend("<option value='0'>Before Apples</option>");

// 옵션변경
$("#myselect").html("<option value='1'>Some oranges</option><option value='2'>More Oranges</option><option value='3'>Even more oranges</option>");

// 인덱스 위치에 따라 옵션 변경
$("#myselect option:eq(1)").replaceWith("<option value='2'>Some apples</option>");
$("#myselect option:eq(2)").replaceWith("<option value='3'>Some bananas</option>");

// 2번째 옵션 셀렉트
$("#myselect option:eq(2)").attr("selected", "selected");

// 해당 텍스트를 가진 옵션 셀렉트
$("#myselect").val("Some oranges").attr("selected", "selected");

// 해당값을 가진 옵션 셀렉트
$("#myselect").val("2");

// 선택 옵션 제거
$("#myselect option:eq(0)").remove();

// 첫번째 옵션 제거
$("#myselect option:first").remove();

// 마지막 옵션 제거
$("#myselect option:last").remove();

// 셀렉트된 옵션 텍스트 리턴
alert($("#myselect option:selected").text());

// 셀렉트된 옵션 값리턴
alert($("#myselect option:selected").val());

// 셀렉트된 인덱스 리턴
alert($("#myselect option").index($("#myselect option:selected")));

// Alternative way to get the selected item
alert($("#myselect option:selected").prevAll().size());

// Insert an item in after a particular position
$("#myselect option:eq(0)").after("<option value='4'>Some pears</option>");

// Insert an item in before a particular position
$("#myselect option:eq(3)").before("<option value='5'>Some apricots</option>");

// Getting values when item is selected
$("#myselect").change(function() {
alert($(this).val());
alert($(this).children("option:selected").text());
});
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,404건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
MySQL 11-10-10 조회 1,935
기타 11-10-10 조회 1,833
기타 11-10-10 조회 1,565
jQuery 11-10-10 조회 2,336
PHP 11-10-08 조회 2,244
Flash 11-10-08 조회 2,423
jQuery 11-10-07 조회 3,237
jQuery 11-10-06 조회 3,655
jQuery 11-10-06 조회 2,080
PHP 11-10-06 조회 4,586
기타 11-10-05 조회 2,438
기타 11-10-05 조회 2,549
Flash 11-10-04 조회 2,054
Flash 11-10-04 조회 6,195
MySQL 11-10-03 조회 2,559
JavaScript 11-10-02 조회 2,322
jQuery 11-10-02 조회 3,705
PHP 11-10-01 조회 1,966
기타 11-09-30 조회 2,102
PHP 11-09-30 조회 2,133
PHP 11-09-29 조회 2,081
JavaScript 11-09-28 조회 1,762
MySQL 11-09-28 조회 2,248
MySQL 11-09-28 조회 4,621
PHP 11-09-26 조회 1,872
MySQL 11-09-26 조회 2,220
MySQL 11-09-26 조회 2,227
MySQL 11-09-25 조회 2,296
JavaScript 11-09-24 조회 2,195
기타 11-09-23 조회 3,814