본문 바로가기

Programming/jQuery

[jQuery] select box 관련

select box 관련



select box id로 선택된 값 읽기

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


select box name으로 선택된 값 읽기

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


선택 값의 index

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


select box에 option값 추가하기

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


select box option의 맨 앞에 추가 할 경우

$("#ID").prepend("<option value='0'>0번</option>");


select box의 html 전체를 변경할 경우

$("#ID").html("<option value='1'>1번</option><option value='2'>2번</option>");


직접 index 값을 주어 selected 속성 주기

$("#ID option:eq(1)").attr("selected, "selected");


text 값으로 selected 속성 주기

$("#ID").val("1번").attr("selected", "selected");


value 값으로 selected 속성 주기

$("#ID").val("1");


해당 index item 삭제하기

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


첫번째, 마지막 item 삭제하기

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

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


선택된 옵션의 text, value, label 구하기

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

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

$("#ID option:selected").attr("label");


선택된 값을 제외한 나머지 option 제거

$("#ID option:not(option:selected)").remove();

$("select[name = NAME] option:not(option:selected)").remove();


첫번째 값을 제외한 나머지 option 제거

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

$("select[name = NAME] option:not(option:eq(0))").remove();


선택된 selectbox 값 가져오기

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

      var str = "";

      $("select option:selected").each(function (idx, item) {

            str += $(this).text() + " ";

      });


      $("div").text(str);

})




'Programming > jQuery' 카테고리의 다른 글

[jQuery] Ajax 전송  (0) 2014.08.14
[jQuery] HTTP Body로 Ajax JSON POST  (0) 2014.08.14
[jQuery] input box 기본문자처리  (0) 2014.08.14
[jQuery] checkbox 관련  (0) 2014.03.19
[jQuery] when() 함수  (0) 2014.03.19