{"id":1665,"date":"2015-08-27T16:39:50","date_gmt":"2015-08-27T07:39:50","guid":{"rendered":"http:\/\/w3devlabs.net\/wp\/?p=1665"},"modified":"2015-08-27T16:39:50","modified_gmt":"2015-08-27T07:39:50","slug":"jquery-select-%ec%a3%bc%eb%ac%b4%eb%a5%b4%ea%b8%b0","status":"publish","type":"post","link":"http:\/\/w3devlabs.net\/wp\/?p=1665","title":{"rendered":"jQuery select \uc8fc\ubb34\ub974\uae30"},"content":{"rendered":"<p>jQuery\ub85c \uc120\ud0dd\ub41c \uac12 \uc77d\uae30<br \/>\n[code language=&#8221;javascript&#8221;]<br \/>\n$(&#8220;#selectBox option:selected&#8221;).val();<br \/>\n$(&#8220;#select_box &gt; option:selected&#8221;).val();<br \/>\n$(&#8220;select[name=name]&#8221;).val();<br \/>\n$(&#8220;selectBox&#8221;).val(); -&gt; \uc694\uac83\ub3c4 \ub098\uc635\ub2c8\ub2e4.<br \/>\n[\/code]<br \/>\njQuery\ub85c \uc120\ud0dd\ub41c \ub0b4\uc6a9 \uc77d\uae30<br \/>\n<code><br \/>\n$(\"#selectBox option:selected\").text();<br \/>\n<\/code><\/p>\n<p>\uc120\ud0dd\ub41c \uc704\uce58<br \/>\n<code><br \/>\nvar index = $(\"#test option\").index($(\"#test option:selected\"));<br \/>\n<\/code><\/p>\n<hr \/>\n<p><code><br \/>\n\/\/ Add options to the end of a select, \ub9e8 \ub4a4\uc5d0 \ucd94\uac00<br \/>\n$(\"#selectBox\").append(\"&lt;option value='1'&gt;Apples&lt;\/option&gt;\");<br \/>\n$(\"#selectBox\").append(\"&lt;option value='2'&gt;After Apples&lt;\/option&gt;\");<\/code><\/p>\n<p>\/\/ Add options to the start of a select, \ub9e8 \uc55e\uc5d0 \ucd94\uac00<br \/>\n$(&#8220;#selectBox&#8221;).prepend(&#8220;&lt;option value=&#8217;0&#8242;&gt;Before Apples&lt;\/option&gt;&#8221;);<\/p>\n<p>\/\/ Replace all the options with new options<br \/>\n$(&#8220;#selectBox&#8221;).html(&#8220;&lt;option value=&#8217;1&#8242;&gt;oranges&lt;\/option&gt;&lt;option value=&#8217;2&#8242;&gt;Oranges&lt;\/option&gt;&#8221;);<\/p>\n<p>\/\/ Replace items at a certain index<br \/>\n$(&#8220;#selectBox option:eq(1)&#8221;).replaceWith(&#8220;&lt;option value=&#8217;2&#8242;&gt;apples&lt;\/option&gt;&#8221;);<br \/>\n$(&#8220;#selectBox option:eq(2)&#8221;).replaceWith(&#8220;&lt;option value=&#8217;3&#8242;&gt;bananas&lt;\/option&gt;&#8221;);<\/p>\n<p>\/\/ \uc9c0\uc815\ub41c index \uac12\uc73c\ub85c select \ud558\uae30<br \/>\n$(&#8220;#selectBox option:eq(2)&#8221;).attr(&#8220;selected&#8221;, &#8220;selected&#8221;);<\/p>\n<p>\/\/ text \uac12\uc73c\ub85c select \ud558\uae30<br \/>\n$(&#8220;#selectBox&#8221;).val(&#8220;Some oranges&#8221;).attr(&#8220;selected&#8221;, &#8220;selected&#8221;);<\/p>\n<p>\/\/ value \uac12\uc73c\ub85c select \ud558\uae30<br \/>\n$(&#8220;#selectBox&#8221;).val(&#8220;2&#8221;);<br \/>\n$(&#8220;#selectBox &gt; option[@value=\uc9c0\uc815\uac12]&#8221;).attr(&#8220;selected&#8221;, &#8220;true&#8221;);<\/p>\n<p>\/\/ \uc9c0\uc815\ub41c \uc778\ub371\uc2a4 \uac12\uc758 item \uc0ad\uc81c<br \/>\n$(&#8220;#selectBox option:eq(0)&#8221;).remove();<\/p>\n<p>\/\/ \uccab\ubc88\uc9f8 item \uc0ad\uc81c<br \/>\n$(&#8220;#selectBox option:first&#8221;).remove();<\/p>\n<p>\/\/ \ub9c8\uc9c0\ub9c9 item \uc0ad\uc81c<br \/>\n$(&#8220;#selectBox option:last&#8221;).remove();<\/p>\n<p>\/\/ \uc120\ud0dd\ub41c \uc635\uc158\uc758 text \uad6c\ud558\uae30<br \/>\nalert($(&#8220;#selectBox option:selected&#8221;).text());<\/p>\n<p>\/\/ \uc120\ud0dd\ub41c \uc635\uc158\uc758 value \uad6c\ud558\uae30<br \/>\nalert($(&#8220;#selectBox option:selected&#8221;).val());<\/p>\n<p>\/\/ \uc120\ud0dd\ub41c \uc635\uc158 index \uad6c\ud558\uae30<br \/>\nalert($(&#8220;#selectBox option&#8221;).index($(&#8220;#selectBox option:selected&#8221;)));<\/p>\n<p>\/\/ SelecBox \uc544\uc774\ud15c \uac2f\uc218 \uad6c\ud558\uae30<br \/>\nalert($(&#8220;#selectBox option&#8221;).size());<\/p>\n<p>\/\/ \uc120\ud0dd\ub41c \uc635\uc158 \uc55e\uc758 \uc544\uc774\ud15c \uac2f\uc218<br \/>\nalert($(&#8220;#selectBox option:selected&#8221;).prevAll().size());<\/p>\n<p>\/\/ \uc120\ud0dd\ub41c \uc635\uc158 \ud6c4\uc758 \uc544\uc774\ud15c \uac2f\uc218<br \/>\nalert($(&#8220;#selectBox option:selected&#8221;).nextAll().size());<\/p>\n<p>\/\/ 0\ubc88\uc9f8 item \ub2e4\uc74c\uc5d0 \uc0bd\uc785<br \/>\n$(&#8220;#selectBox option:eq(0)&#8221;).after(&#8220;&lt;option value=&#8217;4&#8242;&gt;Some pears&lt;\/option&gt;&#8221;);<\/p>\n<p>\/\/ 3\ubc88\uc9f8 item \uc804\uc5d0 \uc0bd\uc785<br \/>\n$(&#8220;#selectBox option:eq(3)&#8221;).before(&#8220;&lt;option value=&#8217;5&#8242;&gt;Some apricots&lt;\/option&gt;&#8221;);<\/p>\n<p>\/\/ select box \uac12\uc774 \ubcc0\uacbd\ub420\ub54c \uc120\ud0dd\ub41c \ud604\uc7ac\uac12<br \/>\n$(&#8220;#selectBox&#8221;).change(function() {<br \/>\nalert($(this).val());<br \/>\nalert($(this).children(&#8220;option:selected&#8221;).text());<br \/>\n});<\/p>\n<p>\ucd9c\ucc98 \ubc0f \ucc38\uc870 : http:\/\/blog.daum.net\/twinsnow\/124<\/p>\n","protected":false},"excerpt":{"rendered":"<p>jQuery\ub85c \uc120\ud0dd\ub41c \uac12 \uc77d\uae30 [code language=&#8221;javascript&#8221;] $(&#8220;#selectBox option:selected&#8221;).val(); $(&#8220;#select_box &gt; option:selected&#8221;).val(); $(&#8220;select[name=name]&#8221;).val(); $(&#8220;selectBox&#8221;).val(); -&gt; \uc694\uac83\ub3c4 \ub098\uc635\ub2c8\ub2e4. [\/code] jQuery\ub85c \uc120\ud0dd\ub41c \ub0b4\uc6a9 \uc77d\uae30 $(&#8220;#selectBox option:selected&#8221;).text(); \uc120\ud0dd\ub41c \uc704\uce58 var index = $(&#8220;#test option&#8221;).index($(&#8220;#test option:selected&#8221;)); \/\/ Add options to the end of a select, \ub9e8 \ub4a4\uc5d0 \ucd94\uac00 $(&#8220;#selectBox&#8221;).append(&#8220;&lt;option value=&#8217;1&#8242;&gt;Apples&lt;\/option&gt;&#8221;); $(&#8220;#selectBox&#8221;).append(&#8220;&lt;option value=&#8217;2&#8242;&gt;After Apples&lt;\/option&gt;&#8221;); \/\/ Add options to the start of &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/w3devlabs.net\/wp\/?p=1665\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;jQuery select \uc8fc\ubb34\ub974\uae30&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[24,25],"tags":[5524,5525,5526],"class_list":["post-1665","post","type-post","status-publish","format-standard","hentry","category-javascript","category-25","tag-control","tag-jquery","tag-select","entry"],"_links":{"self":[{"href":"http:\/\/w3devlabs.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/w3devlabs.net\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/w3devlabs.net\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/w3devlabs.net\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/w3devlabs.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1665"}],"version-history":[{"count":0,"href":"http:\/\/w3devlabs.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1665\/revisions"}],"wp:attachment":[{"href":"http:\/\/w3devlabs.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/w3devlabs.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1665"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/w3devlabs.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}