리자

[펌] 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,548
13년 전 조회 2,451
13년 전 조회 3,521
13년 전 조회 2,688
13년 전 조회 2,859
13년 전 조회 3,022
13년 전 조회 1,885
13년 전 조회 2,072
13년 전 조회 1.8만
13년 전 조회 3,335
13년 전 조회 5,373
13년 전 조회 2,520
13년 전 조회 3,126
13년 전 조회 2,985
13년 전 조회 1.7만
13년 전 조회 1,879
13년 전 조회 1,891
13년 전 조회 1,680
13년 전 조회 2,668
13년 전 조회 4,184
13년 전 조회 4,243
13년 전 조회 3,158
13년 전 조회 6,479
13년 전 조회 3,833
13년 전 조회 6,872
13년 전 조회 2,646
13년 전 조회 2.2만
13년 전 조회 1,890
13년 전 조회 2,426
13년 전 조회 1.8만
13년 전 조회 3,571
13년 전 조회 1,980
13년 전 조회 4,580
13년 전 조회 2,156
13년 전 조회 2,303
13년 전 조회 1,764
13년 전 조회 3,510
13년 전 조회 2,486
13년 전 조회 1,644
13년 전 조회 1,714
13년 전 조회 8,296
13년 전 조회 3,192
13년 전 조회 1,955
13년 전 조회 2,106
13년 전 조회 3,773
13년 전 조회 2,007
13년 전 조회 2만
13년 전 조회 3,838
13년 전 조회 3,664
13년 전 조회 5,716
13년 전 조회 2,943
13년 전 조회 1,766
13년 전 조회 1,981
13년 전 조회 1.7만
13년 전 조회 1,738
13년 전 조회 3,018
13년 전 조회 2,582
13년 전 조회 2,236
13년 전 조회 2,107
13년 전 조회 2,265
13년 전 조회 2.4만
13년 전 조회 3,426
13년 전 조회 3만
13년 전 조회 3,183
13년 전 조회 2.3만
13년 전 조회 6,532
13년 전 조회 1,873
13년 전 조회 1,871
13년 전 조회 3,629
13년 전 조회 2,057
13년 전 조회 3,251
13년 전 조회 2,338
13년 전 조회 2,289
13년 전 조회 7,632
13년 전 조회 6,196
13년 전 조회 2,021
13년 전 조회 2,125
13년 전 조회 5,984
13년 전 조회 7,339
13년 전 조회 2,105
13년 전 조회 1,460
13년 전 조회 1,852
13년 전 조회 2,870
13년 전 조회 6,564
13년 전 조회 1,967
13년 전 조회 2,690
13년 전 조회 2,258
13년 전 조회 1,396
13년 전 조회 2,242
13년 전 조회 1,698
13년 전 조회 1,528
13년 전 조회 7,056
13년 전 조회 1,818
13년 전 조회 1,369
13년 전 조회 3,718
13년 전 조회 2,063
13년 전 조회 4,486
13년 전 조회 2,224
13년 전 조회 1,909
13년 전 조회 1,462