리자

[펌] 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,188건
+
제목 글쓴이 날짜 조회
13-03-03 조회 6,247
13-03-01 조회 5,574
13-03-01 조회 8,318
13-03-01 조회 6,315
13-02-26 조회 3,923
13-02-26 조회 3,867
13-02-26 조회 4,761
13-02-24 조회 8,174
13-02-24 조회 4,259
13-02-24 조회 3,502
13-02-24 조회 5,318
13-02-24 조회 5,309
13-02-24 조회 4,376
13-02-24 조회 5,712
13-02-24 조회 9,402
13-02-14 조회 5,111
13-02-13 조회 9,040
13-01-29 조회 7,688
13-01-24 조회 5,033
13-01-24 조회 4,908
13-01-17 조회 4,317
13-01-16 조회 4,351
13-01-08 조회 4,403
12-12-27 조회 4,061
12-12-20 조회 1.1만
12-12-20 조회 3,383
12-12-06 조회 4,371
12-12-01 조회 4,133
12-11-28 조회 5,699
12-11-25 조회 5,405
12-10-20 조회 4,668
12-10-03 조회 4,831
12-09-26 조회 4,994
12-09-15 조회 8,450
12-09-14 조회 5,463
12-09-11 조회 3,972
12-09-10 조회 3,983
12-09-05 조회 8,863
12-08-29 조회 4,662
12-08-26 조회 3,794
12-08-23 조회 3,494
12-08-22 조회 3,578
12-08-22 조회 4,391
12-08-21 조회 3,537
12-08-20 조회 3,463
12-08-20 조회 9,264
12-08-15 조회 4,381
12-08-13 조회 3,300
12-08-11 조회 4,719
12-08-05 조회 4,569
12-08-03 조회 3,350
12-08-03 조회 4,042
12-08-02 조회 5,024
12-07-29 조회 4,137
12-07-26 조회 3,713
12-07-26 조회 3,583
12-07-22 조회 3,494
12-07-20 조회 3,330
12-07-19 조회 4,192
12-07-19 조회 3,452
12-07-16 조회 3,564
12-07-16 조회 4,485
12-07-15 조회 3,561
12-07-12 조회 7,390
12-07-12 조회 5,035
12-07-11 조회 3,080
12-07-09 조회 3,240
12-07-05 조회 3,853
12-07-04 조회 4,423
12-07-04 조회 3,829
12-07-04 조회 4,350
12-06-29 조회 3,818
12-06-27 조회 3,409
12-06-24 조회 3,881
12-06-22 조회 4,193
12-06-22 조회 8,603
12-06-22 조회 3,258
12-06-21 조회 3,288
12-06-17 조회 3,916
12-06-14 조회 4,016
12-06-14 조회 3,300
12-06-08 조회 4,446
12-06-06 조회 3,409
12-06-04 조회 4,164
12-05-26 조회 4,979
12-05-26 조회 3,935
12-05-25 조회 3,232
12-05-24 조회 3,333
12-05-22 조회 3,379
12-05-20 조회 3,004
12-05-16 조회 3,138
12-05-15 조회 3,534
12-05-10 조회 3,965
12-05-08 조회 3,487
12-05-04 조회 3,342
12-05-03 조회 4,296
12-05-02 조회 3,430
12-05-02 조회 3,205
12-05-02 조회 3,304
12-04-26 조회 3,452