본문 바로가기

study

JQuery Select Box 제어

JQuery Select Box 제어

http://tiger5net.egloos.com/5667935

 

JQuery에 다른 기능을 검색하다가 아래의 사이트를 발견하여 나중에 도움이 될 듯하여 정리해둔다.

 

1. jQuery로 선택된 값 읽기

 

$("#selectBox option:selected").val();

$("select[name=name]").val();

 

 

2. jQuery로 선택된 내용 읽기

 

$("#selectBox option:selected").text();

 

 

3. 선택된 위치

 

var index = $("#test option").index($("#test option:selected"));

 

 

4. Add options to the end of a select

 

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

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

 

 

5. Add options to the start of a select

 

$("#selectBox").prepend("<option value='0'>Before Apples</option>");

 

 

6. Replace all the options with new options

 

$("#selectBox").html("<option value='1'>Some oranges</option><option value='2'>MoreOranges</option>");

 

 

7. Replace items at a certain index

 

$("#selectBox option:eq(1)").replaceWith("<option value='2'>Someapples</option>");

$("#selectBox option:eq(2)").replaceWith("<option value='3'>Somebananas</option>");

 

 

8. 지정된 index값으로 select 하기

 

$("#selectBox option:eq(2)").attr("selected", "selected");

 

 

9. text 값으로 select 하기

 

$("#selectBox").val("Someoranges").attr("selected", "selected");

 

  방법1)

  $("#selectBox option").filter(function() {return this.text == 'searchText';}).prop('selected', 'selected');

 

  방법2)

  $('#selectBox > option').each(function() {

      if($(this).text() == 'searchText') {

         $(this).prop('selected', 'selected');

      }

  });

 

  방법3) 

  $("#selectBox option:contains('searchText')").prop('selected', 'selected');

 

 

10. value값으로 select 하기

 

$("#selectBox").val("2");

 

 

11. 지정된 인덱스값의 item 삭제

 

$("#selectBox option:eq(0)").remove();

 

 

12. 첫번째 item 삭제

 

$("#selectBox option:first").remove();

 

 

13. 마지막 item 삭제

 

$("#selectBox option:last").remove();

 

 

14. 선택된 옵션의 text 구하기

 

alert(!$("#selectBox option:selected").text());

 

 

15. 선택된 옵션의 value 구하기

 

alert(!$("#selectBox option:selected").val());

 

 

16. 선택된 옵션 index 구하기

 

alert(!$("#selectBox option").index($("#selectBox option:selected")));

 

 

17. SelecBox 아이템 갯수 구하기

 

alert(!$("#selectBox option").size());

 

 

18. 선택된 옵션 앞의 아이템 갯수

 

alert(!$("#selectBox option:selected").prevAl!l().size());

 

 

19. 선택된 옵션 후의 아이템 갯수

 

alert(!$("#selectBox option:selected").nextAll().size());

 

 

20. Insert an item in after a particular position

 

$("#selectBox option:eq(0)").after("<option value='4'>Somepears</option>");

 

 

21. Insert an item in before a particular position

 

$("#selectBox option:eq(3)").before("<option value='5'>Someapricots</option>");

 

 

22. Getting values when item is selected

 

$("#selectBox").change(function(){

           alert(!$(this).val());

           alert(!$(this).children("option:selected").text());

});

 

출처 : http://blog.daum.net/twinsnow/124

'study' 카테고리의 다른 글

맥프로 화면 캡쳐  (0) 2019.07.10
윈엠프5.666_full_all.wonju  (0) 2019.06.24
jqurey on 이벤트에 액션  (0) 2017.02.05
jquery input 추가 제거 하기  (0) 2017.02.05
php 게시판 리스트  (0) 2017.01.21