리자

[펌] 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,754
13년 전 조회 1,476
13년 전 조회 2,503
13년 전 조회 1,653
13년 전 조회 1,917
13년 전 조회 2,002
13년 전 조회 1,074
13년 전 조회 1,263
13년 전 조회 1.8만
13년 전 조회 2,323
13년 전 조회 4,352
13년 전 조회 1,676
13년 전 조회 2,101
13년 전 조회 2,155
13년 전 조회 1.6만
13년 전 조회 1,074
13년 전 조회 932
13년 전 조회 719
13년 전 조회 1,614
13년 전 조회 3,194
13년 전 조회 3,448
13년 전 조회 2,116
13년 전 조회 5,653
13년 전 조회 2,809
13년 전 조회 6,022
13년 전 조회 1,631
13년 전 조회 2.1만
13년 전 조회 901
13년 전 조회 1,397
13년 전 조회 1.7만
13년 전 조회 2,782
13년 전 조회 1,026
13년 전 조회 3,736
13년 전 조회 1,188
13년 전 조회 1,343
13년 전 조회 786
13년 전 조회 2,519
13년 전 조회 1,512
13년 전 조회 656
13년 전 조회 903
13년 전 조회 7,467
13년 전 조회 2,400
13년 전 조회 937
13년 전 조회 1,309
13년 전 조회 2,876
13년 전 조회 1,030
13년 전 조회 1.9만
13년 전 조회 2,937
13년 전 조회 2,652
13년 전 조회 4,821
13년 전 조회 1,934
13년 전 조회 811
13년 전 조회 996
13년 전 조회 1.6만
13년 전 조회 809
13년 전 조회 2,214
13년 전 조회 1,769
13년 전 조회 1,305
13년 전 조회 1,164
13년 전 조회 1,297
13년 전 조회 2.3만
13년 전 조회 2,634
13년 전 조회 2.9만
13년 전 조회 2,201
13년 전 조회 2.3만
13년 전 조회 5,735
13년 전 조회 934
13년 전 조회 956
13년 전 조회 2,807
13년 전 조회 1,089
13년 전 조회 2,316
13년 전 조회 1,521
13년 전 조회 1,499
13년 전 조회 6,658
13년 전 조회 5,201
13년 전 조회 1,066
13년 전 조회 1,190
13년 전 조회 5,049
13년 전 조회 6,420
13년 전 조회 1,203
13년 전 조회 535
13년 전 조회 900
13년 전 조회 1,881
13년 전 조회 5,621
13년 전 조회 1,045
13년 전 조회 1,709
13년 전 조회 1,317
13년 전 조회 508
13년 전 조회 1,307
13년 전 조회 780
13년 전 조회 624
13년 전 조회 6,134
13년 전 조회 861
13년 전 조회 462
13년 전 조회 2,801
13년 전 조회 1,180
13년 전 조회 3,540
13년 전 조회 1,358
13년 전 조회 1,068
13년 전 조회 580
🐛 버그신고