리자

[펌] jQuery select box

· 2012-11-09 (금) 09:57:03 · 7456 · 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-07-14 조회 5,381
13-07-14 조회 5,346
13-07-14 조회 4,621
13-07-14 조회 4,225
13-07-14 조회 4,061
13-07-14 조회 4,145
13-07-14 조회 4,867
13-07-14 조회 4,512
13-07-14 조회 4,290
13-07-14 조회 5,336
13-07-14 조회 5,212
13-07-14 조회 4,995
13-07-14 조회 4,713
13-07-14 조회 5,348
13-07-13 조회 3,556
13-07-01 조회 5,581
13-07-01 조회 5,751
13-07-01 조회 3,824
13-07-01 조회 5,189
13-05-27 조회 3,938
13-05-21 조회 4,839
13-05-02 조회 1만
13-04-29 조회 5,169
13-04-15 조회 5,436
13-04-13 조회 6,290
13-02-27 조회 3,814
13-02-26 조회 6,424
13-02-12 조회 4,991
13-02-01 조회 2.3만
13-01-23 조회 3,325
13-01-09 조회 7,968
13-01-07 조회 4,916
13-01-07 조회 3,599
13-01-04 조회 1.3만
13-01-03 조회 1.2만
12-12-31 조회 6,103
12-12-27 조회 4,414
12-12-27 조회 4,050
12-12-27 조회 8,673
12-12-18 조회 3,575
12-12-12 조회 3,655
12-12-12 조회 6,622
12-12-12 조회 8,107
12-12-05 조회 5,229
12-12-01 조회 6,628
12-11-22 조회 4,573
12-11-09 조회 5,391
12-11-09 조회 7,457
12-10-29 조회 4,731
12-10-16 조회 6,268
12-10-09 조회 5,231
12-09-02 조회 4,425
12-09-01 조회 3,514
12-08-30 조회 4,193
12-08-30 조회 4,169
12-08-28 조회 3,511
12-08-21 조회 7,130
12-08-20 조회 3,657
12-08-18 조회 5,048
12-08-18 조회 3,404
12-08-11 조회 3,552
12-08-09 조회 3,653
12-08-07 조회 3,596
12-08-07 조회 4,187
12-08-01 조회 4,535
12-08-01 조회 4,314
12-07-20 조회 9,815
12-07-17 조회 4,078
12-07-15 조회 3,874
12-07-10 조회 4,192
12-07-09 조회 6,901
12-07-05 조회 3,243
12-07-04 조회 3,407
12-06-30 조회 5,667
12-06-28 조회 3,738
12-06-20 조회 3,867
12-06-16 조회 3,113
12-06-14 조회 3,833
12-06-07 조회 3,810
12-06-06 조회 6,093
12-05-27 조회 4,360
12-05-25 조회 3,175
12-05-25 조회 2,935
12-05-24 조회 3,047
12-05-24 조회 4,504
12-05-23 조회 3,869
12-05-22 조회 6,477
12-05-15 조회 3,819
12-05-15 조회 3,608
12-05-14 조회 3,033
12-05-12 조회 3,504
12-05-11 조회 6,926
12-05-07 조회 4,404
12-04-28 조회 3,606
12-04-25 조회 5,218
12-04-15 조회 3,826
12-04-12 조회 4,665
12-04-11 조회 3,414
12-04-08 조회 3,193
12-03-21 조회 6,345