리자

[펌] 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개

댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,187건
+
제목 글쓴이 날짜 조회
12-10-08 조회 4,775
12-10-08 조회 4,565
12-10-08 조회 9,006
12-10-08 조회 4,542
12-10-08 조회 4,428
12-10-07 조회 4,030
12-10-05 조회 5,763
12-10-05 조회 5,853
12-10-05 조회 3,915
12-10-05 조회 4,269
12-10-05 조회 7,564
12-10-04 조회 3,546
12-10-04 조회 3,980
12-10-03 조회 4,711
12-10-03 조회 4,857
12-10-03 조회 4,906
12-10-03 조회 4,535
12-10-03 조회 7,657
12-10-02 조회 3,881
12-10-02 조회 9,182
12-10-02 조회 4,501
12-10-01 조회 4,616
12-09-30 조회 4,330
12-09-29 조회 3,863
12-09-29 조회 4,585
12-09-28 조회 3,528
12-09-28 조회 5,133
12-09-28 조회 3,394
12-09-28 조회 3,293
12-09-27 조회 6,078
12-09-27 조회 3,848
12-09-27 조회 3,466
12-09-27 조회 3,205
12-09-27 조회 3,842
12-09-26 조회 5,045
12-09-26 조회 3,350
12-09-25 조회 4,561
12-09-25 조회 3,420
12-09-25 조회 3,393
12-09-25 조회 5,519
12-09-24 조회 3,720
12-09-24 조회 3,874
12-09-24 조회 3,278
12-09-24 조회 4,278
12-09-24 조회 3,292
12-09-24 조회 3,554
12-09-23 조회 3,159
12-09-23 조회 3,208
12-09-23 조회 3,233
12-09-23 조회 3,367
12-09-22 조회 4,553
12-09-21 조회 3,450
12-09-21 조회 3,591
12-09-21 조회 4,564
12-09-21 조회 3,671
12-09-20 조회 5,391
12-09-20 조회 3,143
12-09-20 조회 6,133
12-09-19 조회 3,349
12-09-18 조회 6,136
12-09-18 조회 3,599
12-09-18 조회 3,702
12-09-18 조회 3,940
12-09-18 조회 2,910
12-09-18 조회 3,009
12-09-18 조회 4,086
12-09-18 조회 4,745
12-09-18 조회 3,274
12-09-18 조회 2,932
12-09-18 조회 3,072
12-09-18 조회 3,216
12-09-18 조회 4,148
12-09-17 조회 3,530
12-09-17 조회 3,031
12-09-17 조회 2,939
12-09-17 조회 3,038
12-09-17 조회 3,281
12-09-17 조회 3,202
12-09-16 조회 3,022
12-09-16 조회 4,106
12-09-16 조회 3,138
12-09-15 조회 8,482
12-09-15 조회 3,645
12-09-14 조회 3,130
12-09-14 조회 4,438
12-09-14 조회 3,005
12-09-14 조회 3,933
12-09-14 조회 5,504
12-09-14 조회 2,960
12-09-14 조회 2,985
12-09-13 조회 3,038
12-09-13 조회 2,958
12-09-13 조회 2,826
12-09-13 조회 3,105
12-09-13 조회 2,930
12-09-13 조회 3,432
12-09-13 조회 3,548
12-09-13 조회 3,111
12-09-12 조회 2,906
12-09-12 조회 3,105