리자

[펌] 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개

댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,187건
+
제목 글쓴이 날짜 조회
12-05-26 조회 4,157
12-05-26 조회 3,482
12-05-26 조회 5,067
12-05-26 조회 4,014
12-05-26 조회 3,541
12-05-25 조회 3,702
12-05-25 조회 3,311
12-05-25 조회 3,217
12-05-25 조회 3,305
12-05-25 조회 3,427
12-05-25 조회 3,259
12-05-25 조회 3,334
12-05-25 조회 3,262
12-05-25 조회 4,078
12-05-25 조회 3,436
12-05-24 조회 3,050
12-05-24 조회 3,019
12-05-24 조회 3,402
12-05-24 조회 3,365
12-05-24 조회 4,717
12-05-24 조회 4,074
12-05-23 조회 3,377
12-05-23 조회 4,150
12-05-23 조회 3,354
12-05-23 조회 4,492
12-05-23 조회 3,661
12-05-22 조회 6,722
12-05-22 조회 3,968
12-05-22 조회 3,484
12-05-22 조회 3,268
12-05-21 조회 3,025
12-05-21 조회 4,020
12-05-21 조회 3,677
12-05-21 조회 5,224
12-05-20 조회 3,118
12-05-19 조회 3,506
12-05-19 조회 3,048
12-05-18 조회 3,105
12-05-18 조회 3,119
12-05-18 조회 3,673
12-05-18 조회 3,282
12-05-18 조회 3,507
12-05-18 조회 5,088
12-05-17 조회 4,132
12-05-17 조회 4,126
12-05-17 조회 4,039
12-05-17 조회 4,826
12-05-16 조회 3,316
12-05-16 조회 3,296
12-05-16 조회 4,556
12-05-16 조회 3,092
12-05-16 조회 3,087
12-05-16 조회 3,471
12-05-16 조회 3,241
12-05-16 조회 3,116
12-05-16 조회 3,217
12-05-16 조회 3,416
12-05-15 조회 3,598
12-05-15 조회 3,236
12-05-15 조회 4,210
12-05-15 조회 3,267
12-05-15 조회 2,948
12-05-15 조회 3,192
12-05-15 조회 3,630
12-05-15 조회 3,360
12-05-15 조회 2,745
12-05-15 조회 3,316
12-05-15 조회 3,758
12-05-14 조회 2,769
12-05-14 조회 2,941
12-05-14 조회 2,910
12-05-14 조회 3,221
12-05-14 조회 3,038
12-05-14 조회 3,204
12-05-13 조회 3,490
12-05-12 조회 3,659
12-05-11 조회 2,951
12-05-11 조회 3,317
12-05-11 조회 7,163
12-05-11 조회 3,348
12-05-11 조회 2,871
12-05-11 조회 2,794
12-05-11 조회 2,871
12-05-11 조회 3,444
12-05-11 조회 2,625
12-05-10 조회 4,012
12-05-10 조회 4,020
12-05-10 조회 2,870
12-05-10 조회 2,793
12-05-10 조회 2,934
12-05-10 조회 4,536
12-05-10 조회 2,578
12-05-10 조회 2,668
12-05-10 조회 2,539
12-05-10 조회 2,811
12-05-10 조회 3,258
12-05-10 조회 3,546
12-05-10 조회 2,790
12-05-10 조회 2,903
12-05-10 조회 3,409