리자

[펌] jQuery select box

· 2012-11-09 (금) 09:57:03 · 7479 · 1

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-02-18 조회 3,516
13-02-15 조회 3,938
13-02-12 조회 5,786
13-02-04 조회 4,078
13-01-30 조회 4,936
13-01-25 조회 3,693
13-01-18 조회 3,756
13-01-18 조회 5,493
13-01-18 조회 6,127
13-01-18 조회 3,779
13-01-18 조회 4,737
13-01-17 조회 4,073
13-01-17 조회 3,569
13-01-17 조회 3,625
13-01-17 조회 3,966
12-12-31 조회 3,552
12-12-28 조회 4,901
12-12-28 조회 5,958
12-12-28 조회 3,402
12-12-27 조회 3,585
12-12-27 조회 4,282
12-12-20 조회 5,226
12-12-18 조회 9,073
12-12-14 조회 4,887
12-12-10 조회 3,641
12-12-10 조회 3,107
12-12-01 조회 3,813
12-11-25 조회 7,304
12-11-22 조회 5,914
12-11-20 조회 2.4만
12-11-19 조회 4,311
12-11-13 조회 3,861
12-11-12 조회 3,864
12-10-23 조회 4,703
12-09-25 조회 4,344
12-09-05 조회 6,937
12-09-04 조회 7,497
12-09-04 조회 3,418
12-08-30 조회 3,391
12-08-30 조회 3,603
12-08-29 조회 3,334
12-08-29 조회 3,155
12-08-26 조회 3,459
12-08-18 조회 3,713
12-08-17 조회 3,986
12-08-16 조회 3,539
12-08-14 조회 3,184
12-08-13 조회 5,588
12-08-13 조회 3,345
12-08-11 조회 3,637
12-08-10 조회 3,207
12-08-07 조회 3,962
12-08-07 조회 3,193
12-08-06 조회 3,410
12-07-31 조회 3,170
12-07-30 조회 3,495
12-07-30 조회 3,124
12-07-29 조회 3,300
12-07-29 조회 3,050
12-07-27 조회 3,106
12-07-21 조회 2,978
12-07-19 조회 3,185
12-07-19 조회 2,774
12-07-17 조회 3,404
12-07-17 조회 3,041
12-07-17 조회 3,945
12-07-16 조회 2,985
12-07-15 조회 3,104
12-07-14 조회 3,157
12-07-13 조회 8,247
12-07-11 조회 3,385
12-07-10 조회 9,947
12-07-09 조회 3,157
12-07-08 조회 4,342
12-07-06 조회 3,628
12-07-05 조회 3,908
12-07-05 조회 3,325
12-07-05 조회 3,207
12-07-03 조회 3,221
12-07-03 조회 3,164
12-07-02 조회 3,207
12-07-02 조회 2,827
12-06-30 조회 3,431
12-06-26 조회 3,140
12-06-22 조회 3,180
12-06-22 조회 3,130
12-06-21 조회 3,102
12-06-21 조회 3,323
12-06-20 조회 3,492
12-06-20 조회 3,160
12-06-20 조회 3,068
12-06-18 조회 3,948
12-06-17 조회 3,380
12-06-15 조회 3,001
12-06-14 조회 2,901
12-06-13 조회 3,504
12-06-11 조회 3,322
12-06-08 조회 3,302
12-06-06 조회 3,157
12-06-06 조회 3,548