리자

[펌] 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,494
13년 전 조회 2,523
13년 전 조회 1,671
13년 전 조회 1,936
13년 전 조회 2,019
13년 전 조회 1,092
13년 전 조회 1,285
13년 전 조회 1.8만
13년 전 조회 2,345
13년 전 조회 4,376
13년 전 조회 1,699
13년 전 조회 2,124
13년 전 조회 2,173
13년 전 조회 1.6만
13년 전 조회 1,095
13년 전 조회 949
13년 전 조회 738
13년 전 조회 1,632
13년 전 조회 3,218
13년 전 조회 3,454
13년 전 조회 2,133
13년 전 조회 5,672
13년 전 조회 2,831
13년 전 조회 6,038
13년 전 조회 1,652
13년 전 조회 2.1만
13년 전 조회 917
13년 전 조회 1,417
13년 전 조회 1.7만
13년 전 조회 2,800
13년 전 조회 1,046
13년 전 조회 3,755
13년 전 조회 1,203
13년 전 조회 1,360
13년 전 조회 805
13년 전 조회 2,539
13년 전 조회 1,528
13년 전 조회 675
13년 전 조회 922
13년 전 조회 7,486
13년 전 조회 2,423
13년 전 조회 956
13년 전 조회 1,330
13년 전 조회 2,895
13년 전 조회 1,048
13년 전 조회 1.9만
13년 전 조회 2,957
13년 전 조회 2,672
13년 전 조회 4,837
13년 전 조회 1,953
13년 전 조회 827
13년 전 조회 1,014
13년 전 조회 1.6만
13년 전 조회 816
13년 전 조회 2,235
13년 전 조회 1,785
13년 전 조회 1,325
13년 전 조회 1,181
13년 전 조회 1,316
13년 전 조회 2.3만
13년 전 조회 2,649
13년 전 조회 2.9만
13년 전 조회 2,220
13년 전 조회 2.3만
13년 전 조회 5,753
13년 전 조회 952
13년 전 조회 968
13년 전 조회 2,825
13년 전 조회 1,111
13년 전 조회 2,332
13년 전 조회 1,537
13년 전 조회 1,521
13년 전 조회 6,676
13년 전 조회 5,217
13년 전 조회 1,086
13년 전 조회 1,210
13년 전 조회 5,066
13년 전 조회 6,437
13년 전 조회 1,218
13년 전 조회 552
13년 전 조회 921
13년 전 조회 1,897
13년 전 조회 5,636
13년 전 조회 1,066
13년 전 조회 1,727
13년 전 조회 1,332
13년 전 조회 518
13년 전 조회 1,322
13년 전 조회 795
13년 전 조회 641
13년 전 조회 6,150
13년 전 조회 881
13년 전 조회 479
13년 전 조회 2,819
13년 전 조회 1,195
13년 전 조회 3,558
13년 전 조회 1,378
13년 전 조회 1,076
13년 전 조회 599
🐛 버그신고