리자

[펌] 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-04-26 조회 3,886
12-04-24 조회 3,266
12-04-23 조회 4,595
12-04-22 조회 3,284
12-04-19 조회 3,379
12-04-18 조회 4,447
12-04-18 조회 3,143
12-04-17 조회 3,413
12-04-17 조회 5,177
12-04-16 조회 3,606
12-04-13 조회 3,162
12-04-09 조회 3,203
12-04-04 조회 3,982
12-04-01 조회 3,264
12-03-31 조회 3,658
12-03-30 조회 3,272
12-03-30 조회 3,928
12-03-26 조회 3,547
12-03-26 조회 3,118
12-03-22 조회 3,271
12-03-21 조회 3,120
12-03-19 조회 3,534
12-03-17 조회 3,576
12-03-14 조회 3,291
12-03-10 조회 3,958
12-03-09 조회 3,569
12-03-09 조회 3,387
12-03-08 조회 3,849
12-03-08 조회 4,012
12-03-08 조회 4,125
12-03-03 조회 3,372
12-03-02 조회 4,699
12-02-28 조회 3,227
12-02-27 조회 3,052
12-02-25 조회 3,193
12-02-24 조회 3,877
12-02-23 조회 3,148
12-02-22 조회 3,680
12-02-22 조회 5,448
12-02-22 조회 4,009
12-02-22 조회 4,328
12-02-18 조회 3,843
12-02-17 조회 3,068
12-02-17 조회 3,466
12-02-14 조회 3,548
12-02-13 조회 3,446
12-02-10 조회 1만
12-02-08 조회 3,088
12-02-07 조회 3,423
12-02-06 조회 3,141
12-02-05 조회 3,616
12-01-31 조회 3,550
12-01-30 조회 9,720
12-01-29 조회 3,883
12-01-26 조회 3,484
12-01-25 조회 4,085
12-01-24 조회 3,494
12-01-10 조회 3,785
12-01-10 조회 4,563
12-01-09 조회 3,311
12-01-08 조회 7,178
12-01-08 조회 3,701
12-01-07 조회 5,233
12-01-04 조회 3,935
12-01-02 조회 3,153
12-01-02 조회 3,201
12-01-02 조회 3,504
12-01-01 조회 3,144
11-12-31 조회 3,459
11-12-31 조회 3,129
11-12-30 조회 3,312
11-12-30 조회 3,467
11-12-29 조회 3,672
11-12-28 조회 3,774
11-12-26 조회 3,352
11-12-23 조회 3,573
11-12-22 조회 3,107
11-12-15 조회 4,198
11-12-08 조회 3,996
11-12-05 조회 3,341
11-12-04 조회 5,750
11-12-03 조회 3,631
11-12-02 조회 3,410
11-12-01 조회 5,490
11-11-30 조회 2,972
11-11-30 조회 2,866
11-11-28 조회 3,217
11-11-26 조회 3,417
11-11-25 조회 3,413
11-11-24 조회 2,992
11-11-22 조회 3,090
11-11-22 조회 2,941
11-11-17 조회 2,871
11-11-11 조회 2,850
11-11-09 조회 2,919
11-11-08 조회 3,302
11-11-08 조회 2,594
11-11-04 조회 3,672
11-11-01 조회 2,980
11-10-26 조회 2,560