jQuery 에서 Select input 에 option 을 추가 삭제하는 코드이다. 영어라 두려워말고 살짝 읽어보면 이해 가능할 것이다.

URL : http://www.electrictoolbox.com/jquery-add-option-select-jquery/


select box의 내용 가져오기
    $("#select_box > option:selected").val();
select box의 값 설정
    $("#select_box > option[@value=지정값]").attr("selected", "true")

My last jQuery post looked at how to count the number of options in a select and then clear all options from the select. This post looks at how to add a new option to a select with jQuery.

Working Example

The following is an example showing an already populated select. Clicking the "Add option" button adds a new option to the end of the select and makes it the selected one. Clicking "Replace options" replaces them with a new set of options and then selects the "Green" one.

Note that the above example will not work if you are reading this in a feed reader. In this case click through to view this post in a web browser.

Adding a single option - by appending HTML

Adding a single option can be done by appending HTML to the select box. The select in the above example has an id of "example" i.e. <select id="example"> and using this method adding a new option by appending HTML can look like this:

1$('#example').append('<option value="foo" selected="selected">Foo</option>');

This newly added option will be the selected option; to change this behavior remove the selected="selected" part.

Adding a single option - by appending a new option - method 1

To avoid having to write and append HTML and stick with a more Javascript approach the new option can be appended with jQuery by creating a new option object. This next example does not work correctly in Internet Explorer; it will add the new option but only the value and not display any text.

1$('#example').append(new Option('Foo', 'foo', true, true));

The true, true part at the end will make this item the selected one.

Adding a single option - by appending a new option - method 2

This method gets a handle to the options and adds a new option to the array of options. This does work in Internet Explorer and is the more traditional Javascript way of adding options.

1var options = $('#example').attr('options');
2options[options.length] = new Option('Foo', 'foo', true, true);

Adding multiple options

Using the final method above this last example shows how to replace the existing set of options with a new set, which may have been retrieved from an AJAX call or similar.

01var newOptions = {
02    'red' : 'Red',
03    'blue' : 'Blue',
04    'green' : 'Green',
05    'yellow' : 'Yellow'
06};
07var selectedOption = 'green';
08 
09var select = $('#example');
10var options = select.attr('options');
11$('option', select).remove();
12 
13$.each(newOptions, function(val, text) {
14    options[options.length] = new Option(text, val);
15});
16select.val(selectedOption);

Lines 1 to 7 define the new options with an associative array and the one which will be selected. This is what might have been retrieved from an AJAX call.

Lines 9 and 10 cache a handles to #example and its options.

Line 11 removes all the existing options.

Lines 13 to 15 loop through the new options and add them to the select box.

And finally line 16 sets the selected option.

Alternative methods

There are some alternative methods to adding options to a select box with jQuery using various plug-ins and I may look at these in a future post.

Related posts:


크리에이티브 커먼즈 라이센스
Creative Commons License
이올린에 북마크하기(0) 이올린에 추천하기(0)

Posted by 홍반장

2010/11/01 09:49 2010/11/01 09:49
, , ,
Response
No Trackback , No Comment
RSS :
http://tcbs17.cafe24.com/tc/rss/response/5603

Trackback URL : http://tcbs17.cafe24.com/tc/trackback/5603

« Previous : 1 : ... 861 : 862 : 863 : 864 : 865 : 866 : 867 : 868 : 869 : ... 6391 : Next »

블로그 이미지

- 홍반장

Archives

Recent Trackbacks

Calendar

«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Statistics Graph

Site Stats

Total hits:
250434
Today:
413
Yesterday:
295