리자

[펌] 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년 전 조회 3,018
13년 전 조회 4,087
13년 전 조회 3,215
13년 전 조회 3,436
13년 전 조회 3,552
13년 전 조회 2,378
13년 전 조회 2,585
13년 전 조회 1.9만
13년 전 조회 3,903
13년 전 조회 5,870
13년 전 조회 3,009
13년 전 조회 3,667
13년 전 조회 3,456
13년 전 조회 1.7만
13년 전 조회 2,370
13년 전 조회 2,455
13년 전 조회 2,234
13년 전 조회 3,174
13년 전 조회 4,713
13년 전 조회 4,700
13년 전 조회 3,686
13년 전 조회 6,968
13년 전 조회 4,346
13년 전 조회 7,327
13년 전 조회 3,157
13년 전 조회 2.2만
13년 전 조회 2,407
13년 전 조회 2,909
13년 전 조회 1.8만
13년 전 조회 4,044
13년 전 조회 2,545
13년 전 조회 5,057
13년 전 조회 2,725
13년 전 조회 2,868
13년 전 조회 2,316
13년 전 조회 4,074
13년 전 조회 3,042
13년 전 조회 2,194
13년 전 조회 2,200
13년 전 조회 8,784
13년 전 조회 3,671
13년 전 조회 2,449
13년 전 조회 2,574
13년 전 조회 4,201
13년 전 조회 2,507
13년 전 조회 2.1만
13년 전 조회 4,243
13년 전 조회 4,181
13년 전 조회 6,125
13년 전 조회 3,434
13년 전 조회 2,288
13년 전 조회 2,459
13년 전 조회 1.7만
13년 전 조회 2,261
13년 전 조회 3,465
13년 전 조회 3,022
13년 전 조회 2,735
13년 전 조회 2,636
13년 전 조회 2,790
13년 전 조회 2.5만
13년 전 조회 3,878
13년 전 조회 3만
13년 전 조회 3,678
13년 전 조회 2.4만
13년 전 조회 6,996
13년 전 조회 2,403
13년 전 조회 2,358
13년 전 조회 4,083
13년 전 조회 2,553
13년 전 조회 3,604
13년 전 조회 2,778
13년 전 조회 2,727
13년 전 조회 8,127
13년 전 조회 6,678
13년 전 조회 2,560
13년 전 조회 2,617
13년 전 조회 6,518
13년 전 조회 7,854
13년 전 조회 2,610
13년 전 조회 1,956
13년 전 조회 2,358
13년 전 조회 3,363
13년 전 조회 7,088
13년 전 조회 2,511
13년 전 조회 3,194
13년 전 조회 2,796
13년 전 조회 1,942
13년 전 조회 2,763
13년 전 조회 2,210
13년 전 조회 2,111
13년 전 조회 7,596
13년 전 조회 2,348
13년 전 조회 1,923
13년 전 조회 4,212
13년 전 조회 2,571
13년 전 조회 4,968
13년 전 조회 2,794
13년 전 조회 2,426
13년 전 조회 1,982