리자

[펌] 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,068
13년 전 조회 4,143
13년 전 조회 3,278
13년 전 조회 3,502
13년 전 조회 3,611
13년 전 조회 2,397
13년 전 조회 2,609
13년 전 조회 1.9만
13년 전 조회 3,958
13년 전 조회 5,933
13년 전 조회 3,027
13년 전 조회 3,720
13년 전 조회 3,469
13년 전 조회 1.7만
13년 전 조회 2,388
13년 전 조회 2,522
13년 전 조회 2,298
13년 전 조회 3,247
13년 전 조회 4,780
13년 전 조회 4,724
13년 전 조회 3,754
13년 전 조회 6,985
13년 전 조회 4,407
13년 전 조회 7,342
13년 전 조회 3,223
13년 전 조회 2.2만
13년 전 조회 2,463
13년 전 조회 2,977
13년 전 조회 1.8만
13년 전 조회 4,066
13년 전 조회 2,597
13년 전 조회 5,073
13년 전 조회 2,769
13년 전 조회 2,925
13년 전 조회 2,376
13년 전 조회 4,142
13년 전 조회 3,106
13년 전 조회 2,243
13년 전 조회 2,212
13년 전 조회 8,803
13년 전 조회 3,688
13년 전 조회 2,515
13년 전 조회 2,585
13년 전 조회 4,226
13년 전 조회 2,567
13년 전 조회 2.1만
13년 전 조회 4,271
13년 전 조회 4,235
13년 전 조회 6,147
13년 전 조회 3,504
13년 전 조회 2,361
13년 전 조회 2,521
13년 전 조회 1.7만
13년 전 조회 2,322
13년 전 조회 3,484
13년 전 조회 3,033
13년 전 조회 2,780
13년 전 조회 2,697
13년 전 조회 2,854
13년 전 조회 2.5만
13년 전 조회 3,896
13년 전 조회 3만
13년 전 조회 3,743
13년 전 조회 2.4만
13년 전 조회 7,005
13년 전 조회 2,462
13년 전 조회 2,422
13년 전 조회 4,096
13년 전 조회 2,613
13년 전 조회 3,626
13년 전 조회 2,798
13년 전 조회 2,738
13년 전 조회 8,184
13년 전 조회 6,749
13년 전 조회 2,616
13년 전 조회 2,691
13년 전 조회 6,582
13년 전 조회 7,914
13년 전 조회 2,665
13년 전 조회 2,021
13년 전 조회 2,412
13년 전 조회 3,447
13년 전 조회 7,152
13년 전 조회 2,575
13년 전 조회 3,260
13년 전 조회 2,857
13년 전 조회 1,992
13년 전 조회 2,833
13년 전 조회 2,270
13년 전 조회 2,165
13년 전 조회 7,662
13년 전 조회 2,412
13년 전 조회 1,986
13년 전 조회 4,280
13년 전 조회 2,631
13년 전 조회 5,038
13년 전 조회 2,858
13년 전 조회 2,486
13년 전 조회 2,037