리자

[펌] 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,779
13년 전 조회 1,495
13년 전 조회 2,525
13년 전 조회 1,673
13년 전 조회 1,936
13년 전 조회 2,023
13년 전 조회 1,093
13년 전 조회 1,285
13년 전 조회 1.8만
13년 전 조회 2,345
13년 전 조회 4,377
13년 전 조회 1,699
13년 전 조회 2,124
13년 전 조회 2,174
13년 전 조회 1.6만
13년 전 조회 1,095
13년 전 조회 950
13년 전 조회 740
13년 전 조회 1,633
13년 전 조회 3,219
13년 전 조회 3,454
13년 전 조회 2,133
13년 전 조회 5,673
13년 전 조회 2,831
13년 전 조회 6,038
13년 전 조회 1,652
13년 전 조회 2.1만
13년 전 조회 918
13년 전 조회 1,417
13년 전 조회 1.7만
13년 전 조회 2,801
13년 전 조회 1,046
13년 전 조회 3,755
13년 전 조회 1,205
13년 전 조회 1,360
13년 전 조회 809
13년 전 조회 2,540
13년 전 조회 1,528
13년 전 조회 676
13년 전 조회 922
13년 전 조회 7,486
13년 전 조회 2,423
13년 전 조회 957
13년 전 조회 1,330
13년 전 조회 2,896
13년 전 조회 1,048
13년 전 조회 1.9만
13년 전 조회 2,958
13년 전 조회 2,673
13년 전 조회 4,840
13년 전 조회 1,956
13년 전 조회 828
13년 전 조회 1,015
13년 전 조회 1.6만
13년 전 조회 816
13년 전 조회 2,236
13년 전 조회 1,785
13년 전 조회 1,326
13년 전 조회 1,181
13년 전 조회 1,317
13년 전 조회 2.3만
13년 전 조회 2,649
13년 전 조회 2.9만
13년 전 조회 2,220
13년 전 조회 2.3만
13년 전 조회 5,753
13년 전 조회 954
13년 전 조회 968
13년 전 조회 2,825
13년 전 조회 1,112
13년 전 조회 2,333
13년 전 조회 1,537
13년 전 조회 1,522
13년 전 조회 6,676
13년 전 조회 5,219
13년 전 조회 1,086
13년 전 조회 1,211
13년 전 조회 5,067
13년 전 조회 6,437
13년 전 조회 1,218
13년 전 조회 552
13년 전 조회 921
13년 전 조회 1,897
13년 전 조회 5,636
13년 전 조회 1,066
13년 전 조회 1,728
13년 전 조회 1,333
13년 전 조회 519
13년 전 조회 1,322
13년 전 조회 795
13년 전 조회 641
13년 전 조회 6,152
13년 전 조회 881
13년 전 조회 480
13년 전 조회 2,819
13년 전 조회 1,195
13년 전 조회 3,558
13년 전 조회 1,378
13년 전 조회 1,076
13년 전 조회 599
🐛 버그신고