{"id":125,"date":"2008-11-25T17:26:52","date_gmt":"2008-11-25T09:26:52","guid":{"rendered":"http:\/\/amjet.dyndns.biz\/blog\/IT\/?p=125"},"modified":"2013-03-01T19:50:14","modified_gmt":"2013-03-01T11:50:14","slug":"javascript-%e8%99%95%e7%90%86-checkbox-%e5%95%8f%e9%a1%8c","status":"publish","type":"post","link":"https:\/\/blog.amjet.biz\/IT\/?p=125","title":{"rendered":"javascript \u8655\u7406 checkbox \u554f\u984c"},"content":{"rendered":"<p>\u5370\u8c61\u4e2d\u4e00\u76f4\u89ba\u5f97 checkbox \u5f88\u96e3\u641e\uff0c\u4f46\u662f\u53c8\u4e0d\u592a\u6e05\u695a\u554f\u984c\u5728\u90a3\uff0c\u4eca\u5929\u7d42\u65bc\u77e5\u9053\u539f\u56e0\u4e86\u3002\u95dc\u9375\u5728\u65bc\u8655\u7406\u7684\u662f\u55ae\u4e00 checkbox \u6216\u662f\u591a\u500b checkbox\uff0c\u8209\u500b\u4f8b\u5b50\uff1a<\/p>\n<p>&lt;form name=&#8221;myform&#8221;&gt;<\/p>\n<p>&lt;input type=&#8221;checkbox&#8221; name=&#8221;food&#8221; value=&#8221;cake&#8221;&gt;<\/p>\n<p>&lt;input type=&#8221;checkbox&#8221; name=&#8221;food&#8221; value=&#8221;noodle&#8221;&gt;<\/p>\n<p>&lt;\/form&gt;<\/p>\n<p>\u8981\u628a 2 \u500b checkbox \u90fd\u52fe\u9078\u8a71\u901a\u5e38\u90fd\u662f\u5229\u7528\u4ee5\u4e0b javascript code<\/p>\n<p>var checkbox=document.forms[&#8216;myform&#8217;].food;<\/p>\n<p>for(i=0;i&lt;checkbox.length;i++) {<\/p>\n<p>checkbox[i].checked=true;<\/p>\n<p>}<\/p>\n<p>\u4f46\u662f\u7576 form \u53ea\u67091\u500b checkbox \u6642\u5c31\u6703\u51fa\u932f\uff0c\u56e0\u70ba\u7576\u53ea\u67091\u500b checkbox \u6642\u7b2c\u4e00\u884c javascript \u53d6\u5f97\u7684\u662f\u4e00\u500b\u7269\u4ef6, \u800c\u4e0d\u662f\u7269\u4ef6\u9663\u5217\uff0c\u53ea\u8981\u57f7\u884c\u5230 checkbox.length \u5c31\u6703\u6709 undefined \u4e4b\u985e\u7684\u932f\u8aa4\uff0c\u6bd4\u8f03\u5b8c\u5584\u7684\u5beb\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>var checkbox=document.forms[&#8216;myform&#8217;].food;<\/p>\n<p>if ( checkbox.length != null ) {<\/p>\n<p>for(i=0;i&lt;checkbox.length;i++) {<\/p>\n<p>checkbox[i].checked=true;<\/p>\n<p>} else {<\/p>\n<p>checkbox.checked=true;<\/p>\n<p>}<\/p>\n<p><strong>[ratings]<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5370\u8c61\u4e2d\u4e00\u76f4\u89ba\u5f97 checkbox \u5f88\u96e3\u641e\uff0c\u4f46\u662f\u53c8\u4e0d\u592a\u6e05\u695a\u554f\u984c\u5728\u90a3\uff0c\u4eca\u5929\u7d42\u65bc\u77e5\u9053\u539f\u56e0\u4e86\u3002\u95dc\u9375\u5728\u65bc\u8655\u7406\u7684\u662f\u55ae\u4e00 checkbox \u6216\u662f\u591a\u500b checkbox\uff0c\u8209\u500b\u4f8b\u5b50\uff1a &lt;form name=&#8221;myform&#8221;&gt; &lt;input type=&#8221;checkbox&#8221; name=&#8221;food&#8221; value=&#8221;cake&#8221;&gt; &lt;input type=&#8221;checkbox&#8221; name=&#8221;food&#8221; value=&#8221;noodle&#8221;&gt; &lt;\/form&gt; \u8981\u628a 2 \u500b checkbox \u90fd\u52fe\u9078\u8a71\u901a\u5e38\u90fd\u662f\u5229\u7528\u4ee5\u4e0b javascript code var checkbox=document.forms[&#8216;myform&#8217;].food; for(i=0;i&lt;checkbox.length;i++) { checkbox[i].checked=true; } \u4f46\u662f\u7576 form \u53ea\u67091\u500b checkbox \u6642\u5c31\u6703\u51fa\u932f\uff0c\u56e0\u70ba\u7576\u53ea\u67091\u500b checkbox \u6642\u7b2c\u4e00\u884c javascript \u53d6\u5f97\u7684\u662f\u4e00\u500b\u7269\u4ef6, \u800c\u4e0d\u662f\u7269\u4ef6\u9663\u5217\uff0c\u53ea\u8981\u57f7\u884c\u5230 checkbox.length \u5c31\u6703\u6709 undefined \u4e4b\u985e\u7684\u932f\u8aa4\uff0c\u6bd4\u8f03\u5b8c\u5584\u7684\u5beb\u6cd5\u5982\u4e0b\uff1a var checkbox=document.forms[&#8216;myform&#8217;].food; if ( checkbox.length != null ) { for(i=0;i&lt;checkbox.length;i++) { [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[46,44],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-webmaster","tag-checkbox","tag-javascript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blog.amjet.biz\/IT\/index.php?rest_route=\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.amjet.biz\/IT\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.amjet.biz\/IT\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.amjet.biz\/IT\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.amjet.biz\/IT\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=125"}],"version-history":[{"count":5,"href":"https:\/\/blog.amjet.biz\/IT\/index.php?rest_route=\/wp\/v2\/posts\/125\/revisions"}],"predecessor-version":[{"id":701,"href":"https:\/\/blog.amjet.biz\/IT\/index.php?rest_route=\/wp\/v2\/posts\/125\/revisions\/701"}],"wp:attachment":[{"href":"https:\/\/blog.amjet.biz\/IT\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.amjet.biz\/IT\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.amjet.biz\/IT\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}