리자

[펌] 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개

댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
13년 전 조회 9,542
13년 전 조회 2,339
13년 전 조회 3,406
13년 전 조회 2,596
13년 전 조회 2,760
13년 전 조회 2,914
13년 전 조회 1,880
13년 전 조회 2,064
13년 전 조회 1.8만
13년 전 조회 3,228
13년 전 조회 5,261
13년 전 조회 2,513
13년 전 조회 3,007
13년 전 조회 2,979
13년 전 조회 1.7만
13년 전 조회 1,876
13년 전 조회 1,791
13년 전 조회 1,582
13년 전 조회 2,595
13년 전 조회 4,067
13년 전 조회 4,213
13년 전 조회 3,079
13년 전 조회 6,474
13년 전 조회 3,718
13년 전 조회 6,865
13년 전 조회 2,539
13년 전 조회 2.2만
13년 전 조회 1,779
13년 전 조회 2,314
13년 전 조회 1.8만
13년 전 조회 3,567
13년 전 조회 1,869
13년 전 조회 4,571
13년 전 조회 2,058
13년 전 조회 2,203
13년 전 조회 1,658
13년 전 조회 3,394
13년 전 조회 2,367
13년 전 조회 1,533
13년 전 조회 1,710
13년 전 조회 8,290
13년 전 조회 3,187
13년 전 조회 1,841
13년 전 조회 2,101
13년 전 조회 3,768
13년 전 조회 1,907
13년 전 조회 2만
13년 전 조회 3,834
13년 전 조회 3,549
13년 전 조회 5,708
13년 전 조회 2,819
13년 전 조회 1,676
13년 전 조회 1,865
13년 전 조회 1.7만
13년 전 조회 1,605
13년 전 조회 3,009
13년 전 조회 2,571
13년 전 조회 2,131
13년 전 조회 2,007
13년 전 조회 2,148
13년 전 조회 2.4만
13년 전 조회 3,416
13년 전 조회 3만
13년 전 조회 3,075
13년 전 조회 2.3만
13년 전 조회 6,527
13년 전 조회 1,751
13년 전 조회 1,747
13년 전 조회 3,621
13년 전 조회 1,931
13년 전 조회 3,242
13년 전 조회 2,333
13년 전 조회 2,285
13년 전 조회 7,510
13년 전 조회 6,087
13년 전 조회 1,904
13년 전 조회 2,007
13년 전 조회 5,889
13년 전 조회 7,213
13년 전 조회 1,993
13년 전 조회 1,339
13년 전 조회 1,747
13년 전 조회 2,751
13년 전 조회 6,468
13년 전 조회 1,866
13년 전 조회 2,579
13년 전 조회 2,133
13년 전 조회 1,285
13년 전 조회 2,124
13년 전 조회 1,589
13년 전 조회 1,404
13년 전 조회 6,933
13년 전 조회 1,707
13년 전 조회 1,235
13년 전 조회 3,604
13년 전 조회 1,943
13년 전 조회 4,391
13년 전 조회 2,118
13년 전 조회 1,776
13년 전 조회 1,330