COMING SOON 🚀

리자

[펌] 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,721
13년 전 조회 3,826
13년 전 조회 2,953
13년 전 조회 3,156
13년 전 조회 3,284
13년 전 조회 2,276
13년 전 조회 2,479
13년 전 조회 1.9만
13년 전 조회 3,631
13년 전 조회 5,628
13년 전 조회 2,915
13년 전 조회 3,392
13년 전 조회 3,353
13년 전 조회 1.7만
13년 전 조회 2,273
13년 전 조회 2,179
13년 전 조회 1,950
13년 전 조회 2,903
13년 전 조회 4,453
13년 전 조회 4,612
13년 전 조회 3,429
13년 전 조회 6,866
13년 전 조회 4,076
13년 전 조회 7,240
13년 전 조회 2,901
13년 전 조회 2.2만
13년 전 조회 2,167
13년 전 조회 2,674
13년 전 조회 1.8만
13년 전 조회 3,945
13년 전 조회 2,273
13년 전 조회 4,971
13년 전 조회 2,440
13년 전 조회 2,598
13년 전 조회 2,032
13년 전 조회 3,790
13년 전 조회 2,789
13년 전 조회 1,952
13년 전 조회 2,117
13년 전 조회 8,691
13년 전 조회 3,585
13년 전 조회 2,214
13년 전 조회 2,493
13년 전 조회 4,098
13년 전 조회 2,287
13년 전 조회 2.1만
13년 전 조회 4,156
13년 전 조회 3,954
13년 전 조회 6,044
13년 전 조회 3,212
13년 전 조회 2,057
13년 전 조회 2,238
13년 전 조회 1.7만
13년 전 조회 2,034
13년 전 조회 3,377
13년 전 조회 2,924
13년 전 조회 2,522
13년 전 조회 2,388
13년 전 조회 2,542
13년 전 조회 2.5만
13년 전 조회 3,783
13년 전 조회 3만
13년 전 조회 3,427
13년 전 조회 2.4만
13년 전 조회 6,901
13년 전 조회 2,152
13년 전 조회 2,144
13년 전 조회 3,985
13년 전 조회 2,302
13년 전 조회 3,512
13년 전 조회 2,684
13년 전 조회 2,627
13년 전 조회 7,876
13년 전 조회 6,445
13년 전 조회 2,286
13년 전 조회 2,362
13년 전 조회 6,246
13년 전 조회 7,580
13년 전 조회 2,360
13년 전 조회 1,719
13년 전 조회 2,091
13년 전 조회 3,117
13년 전 조회 6,818
13년 전 조회 2,246
13년 전 조회 2,960
13년 전 조회 2,539
13년 전 조회 1,676
13년 전 조회 2,508
13년 전 조회 1,962
13년 전 조회 1,843
13년 전 조회 7,325
13년 전 조회 2,085
13년 전 조회 1,633
13년 전 조회 3,972
13년 전 조회 2,297
13년 전 조회 4,714
13년 전 조회 2,506
13년 전 조회 2,168
13년 전 조회 1,728