리자

[펌] 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-08-02 조회 3,893
12-08-02 조회 3,811
12-08-01 조회 4,776
12-08-01 조회 4,670
12-07-31 조회 9,573
12-07-31 조회 3,816
12-07-31 조회 3,362
12-07-31 조회 3,148
12-07-31 조회 3,429
12-07-30 조회 3,611
12-07-30 조회 3,649
12-07-30 조회 3,306
12-07-30 조회 3,768
12-07-30 조회 4,201
12-07-29 조회 3,527
12-07-29 조회 3,266
12-07-29 조회 3,612
12-07-29 조회 4,203
12-07-27 조회 3,307
12-07-26 조회 4,861
12-07-26 조회 3,771
12-07-26 조회 3,374
12-07-26 조회 3,638
12-07-26 조회 4,056
12-07-25 조회 4,151
12-07-25 조회 3,303
12-07-24 조회 4,183
12-07-24 조회 3,669
12-07-23 조회 5,143
12-07-22 조회 3,657
12-07-22 조회 3,558
12-07-21 조회 3,460
12-07-21 조회 4,213
12-07-21 조회 3,183
12-07-20 조회 4,426
12-07-20 조회 3,732
12-07-20 조회 3,470
12-07-20 조회 3,384
12-07-20 조회 1만
12-07-19 조회 3,565
12-07-19 조회 3,393
12-07-19 조회 4,767
12-07-19 조회 4,254
12-07-19 조회 3,258
12-07-19 조회 3,066
12-07-19 조회 4,001
12-07-19 조회 3,517
12-07-19 조회 3,959
12-07-19 조회 5,609
12-07-19 조회 3,542
12-07-18 조회 3,343
12-07-18 조회 5,247
12-07-18 조회 3,540
12-07-18 조회 3,651
12-07-17 조회 4,376
12-07-17 조회 4,384
12-07-17 조회 3,586
12-07-17 조회 3,268
12-07-17 조회 4,140
12-07-17 조회 3,627
12-07-16 조회 3,966
12-07-16 조회 3,633
12-07-16 조회 3,820
12-07-16 조회 3,290
12-07-16 조회 3,289
12-07-16 조회 3,196
12-07-16 조회 4,546
12-07-15 조회 3,599
12-07-15 조회 4,324
12-07-15 조회 3,544
12-07-15 조회 3,524
12-07-15 조회 3,335
12-07-15 조회 3,802
12-07-15 조회 3,684
12-07-15 조회 3,537
12-07-15 조회 3,642
12-07-15 조회 4,231
12-07-15 조회 3,826
12-07-15 조회 4,166
12-07-15 조회 7,104
12-07-15 조회 3,740
12-07-15 조회 5,234
12-07-14 조회 3,890
12-07-14 조회 4,392
12-07-14 조회 3,441
12-07-14 조회 3,292
12-07-13 조회 8,485
12-07-13 조회 3,719
12-07-13 조회 3,683
12-07-12 조회 7,439
12-07-12 조회 4,419
12-07-12 조회 5,090
12-07-12 조회 4,557
12-07-11 조회 3,849
12-07-11 조회 3,566
12-07-11 조회 3,524
12-07-11 조회 3,133
12-07-11 조회 3,575
12-07-10 조회 1만
12-07-10 조회 5,742