리자

[펌] 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년 전 조회 9,015
13년 전 조회 1,735
13년 전 조회 2,770
13년 전 조회 1,913
13년 전 조회 2,156
13년 전 조회 2,259
13년 전 조회 1,325
13년 전 조회 1,517
13년 전 조회 1.8만
13년 전 조회 2,580
13년 전 조회 4,602
13년 전 조회 1,933
13년 전 조회 2,354
13년 전 조회 2,423
13년 전 조회 1.6만
13년 전 조회 1,322
13년 전 조회 1,189
13년 전 조회 976
13년 전 조회 1,873
13년 전 조회 3,444
13년 전 조회 3,656
13년 전 조회 2,373
13년 전 조회 5,921
13년 전 조회 3,057
13년 전 조회 6,275
13년 전 조회 1,890
13년 전 조회 2.1만
13년 전 조회 1,161
13년 전 조회 1,654
13년 전 조회 1.7만
13년 전 조회 3,027
13년 전 조회 1,280
13년 전 조회 3,998
13년 전 조회 1,443
13년 전 조회 1,587
13년 전 조회 1,038
13년 전 조회 2,778
13년 전 조회 1,752
13년 전 조회 917
13년 전 조회 1,157
13년 전 조회 7,724
13년 전 조회 2,654
13년 전 조회 1,195
13년 전 조회 1,556
13년 전 조회 3,118
13년 전 조회 1,281
13년 전 조회 2만
13년 전 조회 3,186
13년 전 조회 2,913
13년 전 조회 5,072
13년 전 조회 2,205
13년 전 조회 1,070
13년 전 조회 1,253
13년 전 조회 1.6만
13년 전 조회 1,015
13년 전 조회 2,450
13년 전 조회 2,008
13년 전 조회 1,545
13년 전 조회 1,424
13년 전 조회 1,554
13년 전 조회 2.4만
13년 전 조회 2,876
13년 전 조회 2.9만
13년 전 조회 2,468
13년 전 조회 2.3만
13년 전 조회 5,996
13년 전 조회 1,197
13년 전 조회 1,175
13년 전 조회 3,061
13년 전 조회 1,352
13년 전 조회 2,570
13년 전 조회 1,773
13년 전 조회 1,760
13년 전 조회 6,901
13년 전 조회 5,458
13년 전 조회 1,319
13년 전 조회 1,449
13년 전 조회 5,303
13년 전 조회 6,663
13년 전 조회 1,445
13년 전 조회 776
13년 전 조회 1,162
13년 전 조회 2,130
13년 전 조회 5,860
13년 전 조회 1,297
13년 전 조회 1,962
13년 전 조회 1,555
13년 전 조회 743
13년 전 조회 1,565
13년 전 조회 1,023
13년 전 조회 863
13년 전 조회 6,380
13년 전 조회 1,127
13년 전 조회 680
13년 전 조회 3,038
13년 전 조회 1,425
13년 전 조회 3,800
13년 전 조회 1,625
13년 전 조회 1,288
13년 전 조회 838