리자

[펌] 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건
+
제목 글쓴이 날짜 조회
16-06-03 조회 3,001
15-07-30 조회 3,399
15-06-08 조회 5,544
15-05-27 조회 4,403
15-01-08 조회 6,695
14-12-30 조회 5,428
14-10-10 조회 5,420
14-08-13 조회 6,332
14-08-11 조회 5,192
14-08-06 조회 6,527
14-03-19 조회 7,422
13-04-26 조회 9,582
12-12-15 조회 5,101
12-12-15 조회 7,337
12-12-11 조회 5,468
12-11-21 조회 6,000
12-10-11 조회 5,082
12-10-03 조회 4,776
12-09-20 조회 6,078
12-09-02 조회 4,255
12-08-29 조회 5,254
12-08-29 조회 4,140
12-08-24 조회 4,240
12-08-18 조회 4,469
12-08-17 조회 4,508
12-08-16 조회 3,764
12-08-06 조회 5,540
12-08-05 조회 4,829
12-08-03 조회 4,545
12-08-03 조회 4,686
12-07-29 조회 3,546
12-07-24 조회 4,132
12-07-22 조회 3,603
12-07-20 조회 4,356
12-07-19 조회 4,705
12-07-19 조회 3,938
12-07-19 조회 3,883
12-07-19 조회 5,548
12-07-19 조회 3,492
12-07-18 조회 5,174
12-07-15 조회 4,145
12-07-14 조회 3,825
12-07-12 조회 4,501
12-07-11 조회 3,774
12-07-11 조회 3,496
12-07-11 조회 3,462
12-07-10 조회 4,643
12-07-09 조회 3,396
12-07-09 조회 4,223
12-07-09 조회 4,638
12-07-08 조회 4,558
12-07-07 조회 5,480
12-07-06 조회 4,737
12-07-06 조회 4,007
12-07-06 조회 3,891
12-07-06 조회 3,819
12-07-02 조회 5,499
12-07-02 조회 4,199
12-06-29 조회 3,723
12-06-28 조회 4,349
12-06-26 조회 3,842
12-06-26 조회 4,963
12-06-21 조회 3,938
12-06-20 조회 4,643
12-06-20 조회 4,270
12-06-15 조회 4,625
12-06-15 조회 4,360
12-06-13 조회 7,378
12-06-05 조회 7,353
12-06-04 조회 3,933
12-06-03 조회 3,777
12-05-31 조회 3,779
12-05-28 조회 6,621
12-05-27 조회 3,855
12-05-25 조회 3,991
12-05-23 조회 4,401
12-05-21 조회 5,108
12-05-17 조회 4,047
12-05-17 조회 3,970
12-05-09 조회 4,042
12-05-07 조회 4,293
12-05-02 조회 4,487
12-05-01 조회 4,661
12-04-30 조회 3,407
12-04-30 조회 4,352
12-04-25 조회 4,437
12-04-25 조회 4,866
12-04-24 조회 3,832
12-04-24 조회 4,599
12-04-23 조회 3,603
12-04-20 조회 3,242
12-04-17 조회 5,431
12-04-16 조회 4,372
12-04-14 조회 3,655
12-04-14 조회 3,683
12-04-14 조회 3,735
12-04-13 조회 3,356
12-04-13 조회 4,542
12-04-13 조회 6,180
12-04-12 조회 3,345