리자

[펌] 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년 전 조회 1만
13년 전 조회 2,945
13년 전 조회 4,023
13년 전 조회 3,143
13년 전 조회 3,358
13년 전 조회 3,463
13년 전 조회 2,332
13년 전 조회 2,548
13년 전 조회 1.9만
13년 전 조회 3,826
13년 전 조회 5,798
13년 전 조회 2,968
13년 전 조회 3,578
13년 전 조회 3,422
13년 전 조회 1.7만
13년 전 조회 2,329
13년 전 조회 2,393
13년 전 조회 2,156
13년 전 조회 3,104
13년 전 조회 4,627
13년 전 조회 4,672
13년 전 조회 3,606
13년 전 조회 6,935
13년 전 조회 4,270
13년 전 조회 7,297
13년 전 조회 3,080
13년 전 조회 2.2만
13년 전 조회 2,328
13년 전 조회 2,838
13년 전 조회 1.8만
13년 전 조회 4,011
13년 전 조회 2,461
13년 전 조회 5,025
13년 전 조회 2,650
13년 전 조회 2,799
13년 전 조회 2,228
13년 전 조회 3,986
13년 전 조회 2,982
13년 전 조회 2,124
13년 전 조회 2,166
13년 전 조회 8,756
13년 전 조회 3,647
13년 전 조회 2,386
13년 전 조회 2,551
13년 전 조회 4,173
13년 전 조회 2,444
13년 전 조회 2.1만
13년 전 조회 4,219
13년 전 조회 4,120
13년 전 조회 6,100
13년 전 조회 3,368
13년 전 조회 2,214
13년 전 조회 2,388
13년 전 조회 1.7만
13년 전 조회 2,200
13년 전 조회 3,432
13년 전 조회 2,986
13년 전 조회 2,669
13년 전 조회 2,559
13년 전 조회 2,719
13년 전 조회 2.5만
13년 전 조회 3,840
13년 전 조회 3만
13년 전 조회 3,589
13년 전 조회 2.4만
13년 전 조회 6,955
13년 전 조회 2,309
13년 전 조회 2,276
13년 전 조회 4,051
13년 전 조회 2,471
13년 전 조회 3,569
13년 전 조회 2,741
13년 전 조회 2,686
13년 전 조회 8,048
13년 전 조회 6,598
13년 전 조회 2,464
13년 전 조회 2,533
13년 전 조회 6,426
13년 전 조회 7,771
13년 전 조회 2,526
13년 전 조회 1,886
13년 전 조회 2,273
13년 전 조회 3,267
13년 전 조회 7,005
13년 전 조회 2,439
13년 전 조회 3,121
13년 전 조회 2,721
13년 전 조회 1,857
13년 전 조회 2,687
13년 전 조회 2,134
13년 전 조회 2,022
13년 전 조회 7,510
13년 전 조회 2,271
13년 전 조회 1,829
13년 전 조회 4,133
13년 전 조회 2,490
13년 전 조회 4,888
13년 전 조회 2,709
13년 전 조회 2,338
13년 전 조회 1,899