리자

[펌] 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-02-18 조회 3,642
13-02-15 조회 4,188
13-02-12 조회 5,962
13-02-04 조회 4,164
13-01-30 조회 5,042
13-01-25 조회 3,817
13-01-18 조회 3,869
13-01-18 조회 5,728
13-01-18 조회 6,224
13-01-18 조회 3,874
13-01-18 조회 4,962
13-01-17 조회 4,345
13-01-17 조회 3,714
13-01-17 조회 3,751
13-01-17 조회 4,087
12-12-31 조회 3,725
12-12-28 조회 5,018
12-12-28 조회 6,138
12-12-28 조회 3,519
12-12-27 조회 3,843
12-12-27 조회 4,417
12-12-20 조회 5,429
12-12-18 조회 9,236
12-12-14 조회 4,972
12-12-10 조회 3,829
12-12-10 조회 3,237
12-12-01 조회 4,102
12-11-25 조회 7,495
12-11-22 조회 6,249
12-11-20 조회 2.4만
12-11-19 조회 4,428
12-11-13 조회 3,997
12-11-12 조회 3,984
12-10-23 조회 4,916
12-09-25 조회 4,512
12-09-05 조회 7,138
12-09-04 조회 7,595
12-09-04 조회 3,514
12-08-30 조회 3,572
12-08-30 조회 3,718
12-08-29 조회 3,471
12-08-29 조회 3,323
12-08-26 조회 3,594
12-08-18 조회 3,894
12-08-17 조회 4,209
12-08-16 조회 3,714
12-08-14 조회 3,396
12-08-13 조회 5,689
12-08-13 조회 3,535
12-08-11 조회 3,836
12-08-10 조회 3,382
12-08-07 조회 4,200
12-08-07 조회 3,442
12-08-06 조회 3,671
12-07-31 조회 3,262
12-07-30 조회 3,566
12-07-30 조회 3,214
12-07-29 조회 3,405
12-07-29 조회 3,171
12-07-27 조회 3,218
12-07-21 조회 3,083
12-07-19 조회 3,298
12-07-19 조회 2,994
12-07-17 조회 3,507
12-07-17 조회 3,190
12-07-17 조회 4,054
12-07-16 조회 3,119
12-07-15 조회 3,234
12-07-14 조회 3,350
12-07-13 조회 8,403
12-07-11 조회 3,495
12-07-10 조회 1만
12-07-09 조회 3,289
12-07-08 조회 4,493
12-07-06 조회 3,765
12-07-05 조회 4,021
12-07-05 조회 3,447
12-07-05 조회 3,413
12-07-03 조회 3,318
12-07-03 조회 3,271
12-07-02 조회 3,308
12-07-02 조회 3,049
12-06-30 조회 3,522
12-06-26 조회 3,250
12-06-22 조회 3,287
12-06-22 조회 3,232
12-06-21 조회 3,244
12-06-21 조회 3,451
12-06-20 조회 3,620
12-06-20 조회 3,274
12-06-20 조회 3,218
12-06-18 조회 4,100
12-06-17 조회 3,520
12-06-15 조회 3,138
12-06-14 조회 3,060
12-06-13 조회 3,614
12-06-11 조회 3,428
12-06-08 조회 3,442
12-06-06 조회 3,305
12-06-06 조회 3,682