리자

[펌] jQuery select box

jQuery로 선택된 값 읽기
$("#select_box option:selected").val();
$("select[name=name]").val();

jQuery로 선택된 내용 읽기
$("#select_box option:selected").text();

선택된 위치
var index = $("#test option").index($("#test option:selected")); 

-------------------------------------------------------------------

// Add options to the end of a select
$("#myselect").append("<option value='1'>Apples</option>");
$("#myselect").append("<option value='2'>After Apples</option>");

// Add options to the start of a select
$("#myselect").prepend("<option value='0'>Before Apples</option>");

// Replace all the options with new options
$("#myselect").html("<option value='1'>Some oranges</option><option value='2'>More Oranges</option><option value='3'>Even more oranges</option>");

// Replace items at a certain index
$("#myselect option:eq(1)").replaceWith("<option value='2'>Some apples</option>");
$("#myselect option:eq(2)").replaceWith("<option value='3'>Some bananas</option>");

// Set the element at index 2 to be selected
$("#myselect option:eq(2)").attr("selected", "selected");

// Set the selected element by text
$("#myselect").val("Some oranges").attr("selected", "selected");

// Set the selected element by value
$("#myselect").val("2");

// Remove an item at a particular index
$("#myselect option:eq(0)").remove();

// Remove first item
$("#myselect option:first").remove();

// Remove last item
$("#myselect option:last").remove();

// Get the text of the selected item
alert($("#myselect option:selected").text());

// Get the value of the selected item
alert($("#myselect option:selected").val());

// Get the index of the selected item
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());
});





|

댓글 1개

댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
13년 전 조회 8,887
13년 전 조회 1,595
13년 전 조회 2,640
13년 전 조회 1,792
13년 전 조회 2,038
13년 전 조회 2,130
13년 전 조회 1,208
13년 전 조회 1,398
13년 전 조회 1.8만
13년 전 조회 2,450
13년 전 조회 4,489
13년 전 조회 1,814
13년 전 조회 2,225
13년 전 조회 2,285
13년 전 조회 1.6만
13년 전 조회 1,205
13년 전 조회 1,050
13년 전 조회 840
13년 전 조회 1,730
13년 전 조회 3,322
13년 전 조회 3,553
13년 전 조회 2,244
13년 전 조회 5,784
13년 전 조회 2,936
13년 전 조회 6,142
13년 전 조회 1,754
13년 전 조회 2.1만
13년 전 조회 1,041
13년 전 조회 1,528
13년 전 조회 1.7만
13년 전 조회 2,900
13년 전 조회 1,154
13년 전 조회 3,865
13년 전 조회 1,317
13년 전 조회 1,465
13년 전 조회 910
13년 전 조회 2,646
13년 전 조회 1,638
13년 전 조회 778
13년 전 조회 1,032
13년 전 조회 7,588
13년 전 조회 2,528
13년 전 조회 1,060
13년 전 조회 1,426
13년 전 조회 2,996
13년 전 조회 1,149
13년 전 조회 1.9만
13년 전 조회 3,067
13년 전 조회 2,792
13년 전 조회 4,946
13년 전 조회 2,058
13년 전 조회 933
13년 전 조회 1,122
13년 전 조회 1.6만
13년 전 조회 911
13년 전 조회 2,336
13년 전 조회 1,881
13년 전 조회 1,420
13년 전 조회 1,283
13년 전 조회 1,423
13년 전 조회 2.4만
13년 전 조회 2,750
13년 전 조회 2.9만
13년 전 조회 2,335
13년 전 조회 2.3만
13년 전 조회 5,851
13년 전 조회 1,063
13년 전 조회 1,060
13년 전 조회 2,919
13년 전 조회 1,210
13년 전 조회 2,434
13년 전 조회 1,633
13년 전 조회 1,622
13년 전 조회 6,772
13년 전 조회 5,329
13년 전 조회 1,185
13년 전 조회 1,322
13년 전 조회 5,169
13년 전 조회 6,531
13년 전 조회 1,321
13년 전 조회 659
13년 전 조회 1,021
13년 전 조회 2,001
13년 전 조회 5,743
13년 전 조회 1,169
13년 전 조회 1,823
13년 전 조회 1,427
13년 전 조회 613
13년 전 조회 1,422
13년 전 조회 886
13년 전 조회 739
13년 전 조회 6,253
13년 전 조회 985
13년 전 조회 572
13년 전 조회 2,912
13년 전 조회 1,295
13년 전 조회 3,661
13년 전 조회 1,492
13년 전 조회 1,176
13년 전 조회 702
🐛 버그신고