{"id":220,"date":"2023-02-23T06:30:21","date_gmt":"2023-02-22T22:30:21","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=220"},"modified":"2023-04-30T15:25:02","modified_gmt":"2023-04-30T07:25:02","slug":"jquery-obtain-value-of-select-option-and-do-operations","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/23\/jquery-obtain-value-of-select-option-and-do-operations\/","title":{"rendered":"jQuery\u83b7\u5f97select\/option\u7684\u503c\u548c\u5bf9select\/option\u8fdb\u884c\u64cd\u4f5c"},"content":{"rendered":"<p>jQuery\u83b7\u53d6Select\u5143\u7d20\uff0c\u5e76\u83b7\u53d6\u9009\u62e9\u7684Text\u548cValue\uff1a<\/p>\n<pre><code class=\"language-javascript\">$(&quot;#select_id&quot;).change(function(){\/\/code...}); \/\/\u4e3aSelect\u6dfb\u52a0\u4e8b\u4ef6\uff0c\u5f53\u9009\u62e9\u5176\u4e2d\u4e00\u9879\u65f6\u89e6\u53d1\nvar checkText = $(&quot;#select_id&quot;).find(&quot;option:selected&quot;).text(); \/\/\u83b7\u53d6Select\u9009\u62e9\u7684Text\nvar checkValue = $(&quot;#select_id&quot;)[0].val(); \/\/\u83b7\u53d6Select\u9009\u62e9\u7684Value\nvar checkIndex = $(&quot;#select_id&quot;)[0].selectedIndex; \/\/\u83b7\u53d6Select\u9009\u62e9\u7684\u7d22\u5f15\u503c\nvar maxIndex = $(&quot;#select_id option:last&quot;).attr(&quot;index&quot;); \/\/\u83b7\u53d6Select\u6700\u5927\u7684\u7d22\u5f15\u503c<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>jQuery\u83b7\u53d6Select\u5143\u7d20\uff0c\u5e76\u8bbe\u7f6e\u7684 Text\u548cValue\uff1a<\/p>\n<pre><code class=\"language-javascript\">$(&quot;#select_id &quot;)[0].selectedIndex = 1; \/\/\u8bbe\u7f6eSelect\u7d22\u5f15\u503c\u4e3a1\u7684\u9879\u9009\u4e2d\n$(&quot;#select_id &quot;)[0].val(&#039;china&#039;); \/\/\u8bbe\u7f6eSelect\u7684Value\u503c\u4e3a&#039;china&#039;\u7684\u9879\u9009\u4e2d\n$(&quot;#select_id &quot;)[0].value = 8; \/\/\u8bbe\u7f6eSelect\u7684Value\u503c\u4e3a8\u7684\u9879\u9009\u4e2d\n$(&quot;#select_id option[text=&#039;jQuery&#039;]&quot;).attr(&quot;selected&quot;, true); \/\/\u8bbe\u7f6eSelect\u7684Text\u503c\u4e3ajQuery\u7684\u9879\u9009\u4e2d<\/code><\/pre>\n<p>jQuery\u6dfb\u52a0\/\u5220\u9664Select\u5143\u7d20\u7684Option\u9879\uff1a<\/p>\n<pre><code class=\"language-javascript\">$(&quot;#select_id&quot;).append(&quot;&lt;option value=&#039;Value&#039;&gt;Text&lt;\/option&gt;&quot;); \/\/\u4e3aSelect\u8ffd\u52a0\u4e00\u4e2aOption(\u4e0b\u62c9\u9879)\n$(&quot;#select_id&quot;).prepend(&quot;&lt;option value=&#039;0&#039;&gt;\u8bf7\u9009\u62e9&lt;\/option&gt;&quot;); \/\/\u4e3aSelect\u63d2\u5165\u4e00\u4e2aOption(\u7b2c\u4e00\u4e2a\u4f4d\u7f6e)\n$(&quot;#select_id option:last&quot;).remove(); \/\/\u5220\u9664Select\u4e2d\u7d22\u5f15\u503c\u6700\u5927Option(\u6700\u540e\u4e00\u4e2a)\n$(&quot;#select_id option[index=&#039;0&#039;]&quot;).remove(); \/\/\u5220\u9664Select\u4e2d\u7d22\u5f15\u503c\u4e3a0\u7684Option(\u7b2c\u4e00\u4e2a)\n$(&quot;#select_id option[value=&#039;1&#039;]&quot;).remove(); \/\/\u5220\u9664Select\u4e2dValue=&#039;1&#039;\u7684Option\n$(&quot;#select_id option[text=&#039;2&#039;]&quot;).remove(); \/\/\u5220\u9664Select\u4e2dText=&#039;2&#039;\u7684Option<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>jQuery\u83b7\u53d6Select\u5143\u7d20\uff0c\u5e76\u83b7\u53d6\u9009\u62e9\u7684Text\u548cValue\uff1a $(&quot;#select_id&#038;q [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[82],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-frontend-basic","tag-jquery"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=220"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/220\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}