{"id":23181,"date":"2024-11-21T09:26:38","date_gmt":"2024-11-21T01:26:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23181\/"},"modified":"2024-11-21T09:26:38","modified_gmt":"2024-11-21T01:26:38","slug":"thinkphp%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%85%a8%e9%80%89%e5%92%8c%e5%88%a0%e9%99%a4%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23181\/","title":{"rendered":"thinkphp\u5982\u4f55\u5b9e\u73b0\u5168\u9009\u548c\u5220\u9664\u529f\u80fd"},"content":{"rendered":"<\/p>\n<p><strong>\u4e00\u3001\u5168\u9009\u529f\u80fd\u5b9e\u73b0<\/strong><\/p>\n<p>1.\u5728\u89c6\u56fe\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a\u5168\u9009\u7684\u6309\u94ae\uff0c\u7c7b\u4f3c\u4e8e\u4e0b\u9762\u8fd9\u6837\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;input&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0ccheck_all()\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5168\u9009\u6216\u53d6\u6d88\u5168\u9009\uff1a<\/p>\n<pre>function&nbsp;check_all(obj){\n&nbsp;&nbsp;&nbsp;$(':checkbox').prop('checked',&nbsp;$(obj).prop('checked'));\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u91cc\uff0c\u7528\u5230\u4e86jQuery\u7684\u9009\u62e9\u5668\u9009\u62e9\u6240\u6709\u7684checkbox\u5e76\u4f7f\u7528prop()\u65b9\u6cd5\u8bbe\u7f6e\u5176checked\u5c5e\u6027\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>2.\u4ee5ThinkPHP\u4e3a\u4f8b\uff0c\u6211\u4eec\u5047\u8bbe\u5df2\u7ecf\u83b7\u53d6\u5230\u4e86\u9700\u8981\u8fdb\u884c\u5168\u9009\u7684\u6570\u636e\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u5230\u89c6\u56fe\u9875\u9762\u4e2d\uff0c\u6b64\u65f6\u6211\u4eec\u9700\u8981\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u6570\u636e\uff0c\u5e76\u4e3a\u5176\u6dfb\u52a0checkbox\u548cID\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>\n  \n&nbsp;&nbsp;&nbsp;&nbsp;\n&lt;input&gt;\"&nbsp;&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;\n  \n\n  <\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u91cc\uff0c\u4e3a\u4e86\u65b9\u4fbf\u64cd\u4f5c\uff0c\u4f7f\u7528\u4e86\u6570\u7ec4\u4f20\u9012checkbox\u7684\u503c\uff0ccheckbox\u7684\u540d\u79f0\u4e3ackb[]\uff0c\u5176\u5bf9\u5e94\u7684value\u4e3a\u8be5\u884c\u6570\u636e\u7684ID\u503c\u3002<\/p>\n<p>3.\u6b64\u65f6\u6211\u4eec\u53ea\u9700\u8981\u5728\u63d0\u4ea4\u8868\u5355\u7684\u65f6\u5019\u83b7\u53d6\u5230\u6240\u6709\u9009\u4e2d\u7684checkbox\u5e76\u5c06\u5176\u5bf9\u5e94\u7684\u503c\u7ec4\u5408\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u5168\u9009\u7684\u529f\u80fd\u4e86\u3002\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>public&nbsp;function&nbsp;all(){\n&nbsp;&nbsp;&nbsp;$ids&nbsp;=&nbsp;input('post.ckb\/a');\n&nbsp;&nbsp;&nbsp;if(empty($ids)){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&amp;gt;error('\u8bf7\u9009\u62e9\u8981\u5220\u9664\u7684\u6570\u636e\uff01');\n&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;$ids&nbsp;=&nbsp;implode(',',$ids);\n&nbsp;&nbsp;&nbsp;$where['id']&nbsp;=&nbsp;array('in',$ids);\n&nbsp;&nbsp;&nbsp;$result&nbsp;=&nbsp;db('table')-&amp;gt;where($where)-&amp;gt;delete();\n&nbsp;&nbsp;&nbsp;if($result){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&amp;gt;success('\u5220\u9664\u6210\u529f\uff01');\n&nbsp;&nbsp;&nbsp;}else{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&amp;gt;error('\u5220\u9664\u5931\u8d25\uff01');\n&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u91cc\uff0cinput(&#8216;post.ckb\/a&#8217;)\u7528\u4e8e\u83b7\u53d6\u63d0\u4ea4\u8868\u5355\u65f6\u4f20\u9012\u7684\u6240\u6709checkbox\u7684\u503c\uff0c\u4f7f\u7528implode()\u65b9\u6cd5\u5c06\u5176\u8fde\u63a5\u6210\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u7528\u4e8e\u67e5\u8be2\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u4e8c\u3001\u6279\u91cf\u5220\u9664\u529f\u80fd\u5b9e\u73b0<\/strong><\/p>\n<p>\u5b9e\u73b0\u6279\u91cf\u5220\u9664\u529f\u80fd\u9700\u8981\u7ed3\u5408\u524d\u9762\u7684\u5168\u9009\u529f\u80fd\uff0c\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p>1.\u9996\u5148\uff0c\u7528\u6237\u9700\u8981\u9009\u4e2d\u9700\u8981\u5220\u9664\u7684\u6570\u636e\uff0c\u7136\u540e\u70b9\u51fb\u5220\u9664\u6309\u94ae\uff08\u6216\u5176\u4ed6\u81ea\u5b9a\u4e49\u6309\u94ae\uff09\uff0c\u8fd9\u65f6\u9700\u8981\u83b7\u53d6\u9009\u4e2d\u7684\u6570\u636e\u5e76\u5bf9\u5176\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n<p>2.\u4e3a\u4e86\u65b9\u4fbf\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6240\u6709\u9009\u4e2d\u7684\u6570\u636eID\u503c\u5408\u5e76\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff08\u4ee5\u82f1\u6587\u9017\u53f7\u5206\u9694\uff09\uff0c\u7136\u540e\u5c06\u5176\u4f20\u9012\u5230\u4e0b\u4e00\u4e2a\u5904\u7406\u51fd\u6570\u4e2d\u3002<\/p>\n<p>3.\u4f7f\u7528where()\u51fd\u6570\u5c06\u5220\u9664\u6761\u4ef6\u8bbe\u7f6e\u4e3aid in (ids)\uff08\u5176\u4e2d\uff0cids\u4e3a\u6240\u6709\u8981\u5220\u9664\u6570\u636e\u7684ID\uff09\uff0c\u7136\u540e\u4f7f\u7528delete()\u51fd\u6570\u5220\u9664\u6ee1\u8db3\u6761\u4ef6\u7684\u6570\u636e\u5373\u53ef\u3002<\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>public&nbsp;function&nbsp;delete(){\n&nbsp;&nbsp;&nbsp;$ids&nbsp;=&nbsp;input('post.ids\/s','');\n&nbsp;&nbsp;&nbsp;if(empty($ids)){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&amp;gt;error('\u8bf7\u9009\u62e9\u8981\u5220\u9664\u7684\u6570\u636e\uff01');\n&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;$where['id']&nbsp;=&nbsp;array('in',$ids);\n&nbsp;&nbsp;&nbsp;$result&nbsp;=&nbsp;db('table')-&amp;gt;where($where)-&amp;gt;delete();\n&nbsp;&nbsp;&nbsp;if($result){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&amp;gt;success('\u5220\u9664\u6210\u529f\uff01');\n&nbsp;&nbsp;&nbsp;}else{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&amp;gt;error('\u5220\u9664\u5931\u8d25\uff01');\n&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5b9e\u73b0\u5168\u9009\u548c\u5220\u9664\u529f\u80fd\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5168\u9009\u529f\u80fd\u5b9e\u73b0 1.\u5728\u89c6\u56fe\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a\u5168\u9009\u7684\u6309\u94ae\uff0c\u7c7b\u4f3c\u4e8e\u4e0b\u9762\u8fd9\u6837\u7684\u4ee3\u7801\uff1a &lt;input&gt; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0ccheck_all()\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5168\u9009\u6216\u53d6\u6d88\u5168\u9009\uff1a function&nbsp;check_all(obj){ &nbsp;&nbsp;&nbsp;$(&#8216;:checkbox&#8217;).prop(&#8216;checked&#8217;,&nbsp;$(obj).prop(&#8216;checked&#8217;)); } \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u91cc\uff0c\u7528\u5230\u4e86jQuery\u7684\u9009\u62e9\u5668\u9009\u62e9\u6240\u6709\u7684checkbox\u5e76\u4f7f\u7528prop()\u65b9\u6cd5\u8bbe\u7f6e\u5176checked\u5c5e\u6027\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 2.\u4ee5ThinkPHP\u4e3a\u4f8b\uff0c\u6211\u4eec\u5047\u8bbe\u5df2\u7ecf\u83b7\u53d6\u5230\u4e86\u9700\u8981\u8fdb\u884c\u5168\u9009\u7684\u6570\u636e\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u5230\u89c6\u56fe\u9875\u9762\u4e2d\uff0c\u6b64\u65f6\u6211\u4eec\u9700\u8981\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u6570\u636e\uff0c\u5e76\u4e3a\u5176\u6dfb\u52a0checkbox\u548cID\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a &nbsp;&nbsp;&nbsp;&nbsp; &lt;input&gt;&#8221;&nbsp;&gt; &nbsp;&nbsp;&nbsp;&nbsp; \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u91cc\uff0c\u4e3a\u4e86\u65b9\u4fbf\u64cd\u4f5c\uff0c\u4f7f\u7528\u4e86\u6570\u7ec4\u4f20\u9012checkbox\u7684\u503c\uff0ccheckbox\u7684\u540d\u79f0\u4e3ackb[]\uff0c\u5176\u5bf9\u5e94\u7684value\u4e3a\u8be5\u884c\u6570\u636e\u7684ID\u503c\u3002 3.\u6b64\u65f6\u6211\u4eec\u53ea\u9700\u8981\u5728\u63d0\u4ea4\u8868\u5355\u7684\u65f6\u5019\u83b7\u53d6\u5230\u6240\u6709\u9009\u4e2d\u7684checkbox\u5e76\u5c06\u5176\u5bf9\u5e94\u7684\u503c\u7ec4\u5408\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u5168\u9009\u7684\u529f\u80fd\u4e86\u3002\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a public&nbsp;function&nbsp;all(){ &nbsp;&nbsp;&nbsp;$ids&nbsp;=&nbsp;input(&#8216;post.ckb\/a&#8217;); &nbsp;&nbsp;&nbsp;if(empty($ids)){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&amp;gt;error(&#8216;\u8bf7\u9009\u62e9\u8981\u5220\u9664\u7684\u6570\u636e\uff01&#8217;); &nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;$ids&nbsp;=&nbsp;implode(&#8216;,&#8217;,$ids); &nbsp;&nbsp;&nbsp;$where[&#8216;id&#8217;]&nbsp;=&nbsp;array(&#8216;in&#8217;,$ids); &nbsp;&nbsp;&nbsp;$result&nbsp;=&nbsp;db(&#8216;table&#8217;)-&amp;gt;where($where)-&amp;gt;delete(); &nbsp;&nbsp;&nbsp;if($result){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&amp;gt;success(&#8216;\u5220\u9664\u6210\u529f\uff01&#8217;); &nbsp;&nbsp;&nbsp;}else{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&amp;gt;error(&#8216;\u5220\u9664\u5931\u8d25\uff01&#8217;); &nbsp;&nbsp;&nbsp;} } \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u91cc\uff0cinput(&#8216;post.ckb\/a&#8217;)\u7528\u4e8e\u83b7\u53d6\u63d0\u4ea4\u8868\u5355\u65f6\u4f20\u9012\u7684\u6240\u6709checkbox\u7684\u503c\uff0c\u4f7f\u7528implode()\u65b9\u6cd5\u5c06\u5176\u8fde\u63a5\u6210\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u7528\u4e8e\u67e5\u8be2\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u3002 \u4e8c\u3001\u6279\u91cf\u5220\u9664\u529f\u80fd\u5b9e\u73b0 \u5b9e\u73b0\u6279\u91cf\u5220\u9664\u529f\u80fd\u9700\u8981\u7ed3\u5408\u524d\u9762\u7684\u5168\u9009\u529f\u80fd\uff0c\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a 1.\u9996\u5148\uff0c\u7528\u6237\u9700\u8981\u9009\u4e2d\u9700\u8981\u5220\u9664\u7684\u6570\u636e\uff0c\u7136\u540e\u70b9\u51fb\u5220\u9664\u6309\u94ae\uff08\u6216\u5176\u4ed6\u81ea\u5b9a\u4e49\u6309\u94ae\uff09\uff0c\u8fd9\u65f6\u9700\u8981\u83b7\u53d6\u9009\u4e2d\u7684\u6570\u636e\u5e76\u5bf9\u5176\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\u3002 2.\u4e3a\u4e86\u65b9\u4fbf\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6240\u6709\u9009\u4e2d\u7684\u6570\u636eID\u503c\u5408\u5e76\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff08\u4ee5\u82f1\u6587\u9017\u53f7\u5206\u9694\uff09\uff0c\u7136\u540e\u5c06\u5176\u4f20\u9012\u5230\u4e0b\u4e00\u4e2a\u5904\u7406\u51fd\u6570\u4e2d\u3002 3.\u4f7f\u7528where()\u51fd\u6570\u5c06\u5220\u9664\u6761\u4ef6\u8bbe\u7f6e\u4e3aid in (ids)\uff08\u5176\u4e2d\uff0cids\u4e3a\u6240\u6709\u8981\u5220\u9664\u6570\u636e\u7684ID\uff09\uff0c\u7136\u540e\u4f7f\u7528delete()\u51fd\u6570\u5220\u9664\u6ee1\u8db3\u6761\u4ef6\u7684\u6570\u636e\u5373\u53ef\u3002 \u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a public&nbsp;function&nbsp;delete(){ &nbsp;&nbsp;&nbsp;$ids&nbsp;=&nbsp;input(&#8216;post.ids\/s&#8217;,&#8221;); &nbsp;&nbsp;&nbsp;if(empty($ids)){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&amp;gt;error(&#8216;\u8bf7\u9009\u62e9\u8981\u5220\u9664\u7684\u6570\u636e\uff01&#8217;); &nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;$where[&#8216;id&#8217;]&nbsp;=&nbsp;array(&#8216;in&#8217;,$ids); &nbsp;&nbsp;&nbsp;$result&nbsp;=&nbsp;db(&#8216;table&#8217;)-&amp;gt;where($where)-&amp;gt;delete(); &nbsp;&nbsp;&nbsp;if($result){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&amp;gt;success(&#8216;\u5220\u9664\u6210\u529f\uff01&#8217;); &nbsp;&nbsp;&nbsp;}else{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&amp;gt;error(&#8216;\u5220\u9664\u5931\u8d25\uff01&#8217;); &nbsp;&nbsp;&nbsp;} } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-23181","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=23181"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23181\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}