리자

[펌] 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,849
13년 전 조회 3,930
13년 전 조회 3,056
13년 전 조회 3,263
13년 전 조회 3,385
13년 전 조회 2,316
13년 전 조회 2,522
13년 전 조회 1.9만
13년 전 조회 3,738
13년 전 조회 5,724
13년 전 조회 2,951
13년 전 조회 3,505
13년 전 조회 3,400
13년 전 조회 1.7만
13년 전 조회 2,313
13년 전 조회 2,293
13년 전 조회 2,054
13년 전 조회 3,010
13년 전 조회 4,552
13년 전 조회 4,641
13년 전 조회 3,519
13년 전 조회 6,913
13년 전 조회 4,187
13년 전 조회 7,277
13년 전 조회 3,000
13년 전 조회 2.2만
13년 전 조회 2,252
13년 전 조회 2,769
13년 전 조회 1.8만
13년 전 조회 3,987
13년 전 조회 2,373
13년 전 조회 5,008
13년 전 조회 2,557
13년 전 조회 2,707
13년 전 조회 2,144
13년 전 조회 3,910
13년 전 조회 2,890
13년 전 조회 2,037
13년 전 조회 2,151
13년 전 조회 8,733
13년 전 조회 3,622
13년 전 조회 2,307
13년 전 조회 2,529
13년 전 조회 4,147
13년 전 조회 2,364
13년 전 조회 2.1만
13년 전 조회 4,194
13년 전 조회 4,037
13년 전 조회 6,078
13년 전 조회 3,288
13년 전 조회 2,140
13년 전 조회 2,318
13년 전 조회 1.7만
13년 전 조회 2,129
13년 전 조회 3,409
13년 전 조회 2,964
13년 전 조회 2,599
13년 전 조회 2,477
13년 전 조회 2,637
13년 전 조회 2.5만
13년 전 조회 3,817
13년 전 조회 3만
13년 전 조회 3,502
13년 전 조회 2.4만
13년 전 조회 6,929
13년 전 조회 2,236
13년 전 조회 2,219
13년 전 조회 4,027
13년 전 조회 2,395
13년 전 조회 3,546
13년 전 조회 2,716
13년 전 조회 2,666
13년 전 조회 7,959
13년 전 조회 6,523
13년 전 조회 2,395
13년 전 조회 2,461
13년 전 조회 6,344
13년 전 조회 7,686
13년 전 조회 2,450
13년 전 조회 1,817
13년 전 조회 2,195
13년 전 조회 3,191
13년 전 조회 6,919
13년 전 조회 2,355
13년 전 조회 3,048
13년 전 조회 2,633
13년 전 조회 1,787
13년 전 조회 2,604
13년 전 조회 2,055
13년 전 조회 1,948
13년 전 조회 7,426
13년 전 조회 2,182
13년 전 조회 1,739
13년 전 조회 4,062
13년 전 조회 2,414
13년 전 조회 4,801
13년 전 조회 2,624
13년 전 조회 2,266
13년 전 조회 1,832