리자

[펌] 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,905
13년 전 조회 1,609
13년 전 조회 2,657
13년 전 조회 1,808
13년 전 조회 2,050
13년 전 조회 2,144
13년 전 조회 1,219
13년 전 조회 1,413
13년 전 조회 1.8만
13년 전 조회 2,464
13년 전 조회 4,502
13년 전 조회 1,829
13년 전 조회 2,241
13년 전 조회 2,303
13년 전 조회 1.6만
13년 전 조회 1,219
13년 전 조회 1,071
13년 전 조회 861
13년 전 조회 1,745
13년 전 조회 3,338
13년 전 조회 3,563
13년 전 조회 2,257
13년 전 조회 5,805
13년 전 조회 2,953
13년 전 조회 6,156
13년 전 조회 1,771
13년 전 조회 2.1만
13년 전 조회 1,051
13년 전 조회 1,540
13년 전 조회 1.7만
13년 전 조회 2,916
13년 전 조회 1,170
13년 전 조회 3,883
13년 전 조회 1,334
13년 전 조회 1,478
13년 전 조회 924
13년 전 조회 2,657
13년 전 조회 1,650
13년 전 조회 798
13년 전 조회 1,046
13년 전 조회 7,605
13년 전 조회 2,542
13년 전 조회 1,074
13년 전 조회 1,451
13년 전 조회 3,015
13년 전 조회 1,163
13년 전 조회 1.9만
13년 전 조회 3,086
13년 전 조회 2,802
13년 전 조회 4,968
13년 전 조회 2,085
13년 전 조회 957
13년 전 조회 1,140
13년 전 조회 1.6만
13년 전 조회 926
13년 전 조회 2,350
13년 전 조회 1,893
13년 전 조회 1,433
13년 전 조회 1,300
13년 전 조회 1,443
13년 전 조회 2.4만
13년 전 조회 2,762
13년 전 조회 2.9만
13년 전 조회 2,347
13년 전 조회 2.3만
13년 전 조회 5,873
13년 전 조회 1,075
13년 전 조회 1,078
13년 전 조회 2,933
13년 전 조회 1,229
13년 전 조회 2,459
13년 전 조회 1,648
13년 전 조회 1,632
13년 전 조회 6,791
13년 전 조회 5,343
13년 전 조회 1,204
13년 전 조회 1,343
13년 전 조회 5,185
13년 전 조회 6,550
13년 전 조회 1,336
13년 전 조회 675
13년 전 조회 1,041
13년 전 조회 2,011
13년 전 조회 5,758
13년 전 조회 1,180
13년 전 조회 1,840
13년 전 조회 1,445
13년 전 조회 631
13년 전 조회 1,444
13년 전 조회 913
13년 전 조회 761
13년 전 조회 6,271
13년 전 조회 1,003
13년 전 조회 586
13년 전 조회 2,931
13년 전 조회 1,319
13년 전 조회 3,679
13년 전 조회 1,503
13년 전 조회 1,192
13년 전 조회 719
🐛 버그신고