{"id":170292,"date":"2024-05-08T17:04:38","date_gmt":"2024-05-08T09:04:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/170292.html"},"modified":"2024-05-08T17:04:41","modified_gmt":"2024-05-08T09:04:41","slug":"javascript-%e7%a8%8b%e5%ba%8f%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e7%ba%a7%e8%81%94%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/170292.html","title":{"rendered":"JavaScript \u7a0b\u5e8f\u600e\u4e48\u5b9e\u73b0\u4e00\u4e2a\u7ea7\u8054\u7b97\u6cd5"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27041024\/b08f1642-a73b-422c-a7af-91a1c0e330da.webp\" alt=\"JavaScript \u7a0b\u5e8f\u600e\u4e48\u5b9e\u73b0\u4e00\u4e2a\u7ea7\u8054\u7b97\u6cd5\" \/><\/p>\n<p><p><strong>JavaScript\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u7ea7\u8054\u7b97\u6cd5\u901a\u5e38\u6d89\u53ca\u5230\u5c06\u8f93\u5165\u5206\u6210\u591a\u7ea7\u5904\u7406\u3001\u5904\u7406\u5b50\u7ea7\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u3001\u4ee5\u53ca\u6700\u540e\u7efc\u5408\u5404\u4e2a\u7ea7\u522b\u7684\u7ed3\u679c\u5f97\u51fa\u6700\u7ec8\u8f93\u51fa<\/strong>\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u7ea7\u8054\u7b97\u6cd5\u4e3b\u8981\u5305\u62ec\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\u4ee5\u5b58\u50a8\u591a\u7ea7\u4fe1\u606f\u3001\u901a\u8fc7\u51fd\u6570\u9012\u5f52\u6216\u5faa\u73af\u6765\u5904\u7406\u6bcf\u4e00\u5c42\u7684\u6570\u636e\u3001\u4ee5\u53ca\u5728\u5404\u5c42\u4e4b\u95f4\u4f20\u9012\u72b6\u6001\u4fe1\u606f\u3002<strong>\u4f8b\u5982\uff0c\u7ea7\u8054\u9009\u62e9\u6846\u6d89\u53ca\u5230\u7528\u6237\u9009\u62e9\u4e00\u4e2a\u9009\u9879\u540e\uff0c\u89e6\u53d1\u4e0b\u4e00\u7ea7\u9009\u62e9\u6846\u5185\u5bb9\u7684\u52a8\u6001\u66f4\u65b0<\/strong>\u3002\u5728\u5b9e\u73b0\u8fd9\u6837\u7684\u7b97\u6cd5\u65f6\uff0c\u6838\u5fc3\u662f\u7ba1\u7406\u72b6\u6001\u548c\u6570\u636e\u6d41\u5411\uff0c\u5e76\u786e\u4fdd\u6570\u636e\u7684\u6b63\u786e\u6027\u548c\u7b97\u6cd5\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u7ea7\u8054\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u8bbe\u8ba1\u7ea7\u8054\u7b97\u6cd5\u4e4b\u524d\uff0c\u9996\u5148\u8981\u5b9a\u4e49\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u7ea7\u8054\u4e2d\u7684\u5404\u7ea7\u6570\u636e\u3002\u8fd9\u901a\u5e38\u662f\u4e00\u4e2a\u5305\u542b\u5b50\u7ed3\u70b9\u7684\u6811\u72b6\u7ed3\u6784\u3002\u5bf9\u4e8e\u7ea7\u8054\u9009\u62e9\u6846\u800c\u8a00\uff0c\u53ef\u4ee5\u7528\u6570\u7ec4\u8868\u793a\u5404\u7ea7\u522b\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u6570\u7ec4\u9879\u4e3a\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5305\u542b\u8be5\u7ea7\u522b\u7684\u5217\u8868\u548c\u5f53\u524d\u9009\u4e2d\u7684\u9879\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let cascadeData = [<\/p>\n<p>    {<\/p>\n<p>        level: 1,<\/p>\n<p>        items: [&#039;\u9009\u98791-1&#039;, &#039;\u9009\u98791-2&#039;],<\/p>\n<p>        selected: null,<\/p>\n<p>    },<\/p>\n<p>    {<\/p>\n<p>        level: 2,<\/p>\n<p>        items: [],<\/p>\n<p>        selected: null,<\/p>\n<p>    }<\/p>\n<p>    \/\/ \u53ef\u4ee5\u6709\u66f4\u591a\u7ea7\u522b<\/p>\n<p>];<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5904\u7406\u7528\u6237\u4ea4\u4e92<\/h3>\n<\/p>\n<p><p>\u5728\u7528\u6237\u4e0e\u7ea7\u8054\u7b97\u6cd5\u4e92\u52a8\u65f6\uff0c\u4f8b\u5982\u9009\u62e9\u4e86\u4e00\u4e2a\u9009\u9879\uff0c\u7a0b\u5e8f\u5e94\u8be5\u6839\u636e\u5f53\u524d\u9009\u62e9\u7684\u5185\u5bb9\u66f4\u65b0\u4e0b\u4e00\u7ea7\u7684\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function updateNextLevel(currentLevel, selectedIndex) {<\/p>\n<p>    const nextLevelIndex = currentLevel + 1;<\/p>\n<p>    \/\/ \u5047\u8bbe nextLevelItems() \u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u6839\u636e\u5f53\u524d\u9009\u62e9\u8fd4\u56de\u4e0b\u4e00\u7ea7\u7684\u9009\u9879<\/p>\n<p>    const nextItems = nextLevelItems(cascadeData[currentLevel].items[selectedIndex]);<\/p>\n<p>    if (cascadeData[nextLevelIndex]) {<\/p>\n<p>        cascadeData[nextLevelIndex].items = nextItems;<\/p>\n<p>        cascadeData[nextLevelIndex].selected = null;<\/p>\n<p>        \/\/ \u9012\u5f52\u66f4\u65b0\u540e\u7eed\u7ea7\u522b<\/p>\n<p>        updateNextLevel(nextLevelIndex, 0);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u9012\u5f52\u5904\u7406\u4f9d\u8d56<\/h3>\n<\/p>\n<p><p>\u7ea7\u8054\u7b97\u6cd5\u4e2d\u5e38\u5e38\u9700\u8981\u5904\u7406\u5c42\u4e0e\u5c42\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u3002\u9012\u5f52\u662f\u5904\u7406\u8fd9\u79cd\u5c42\u7ea7\u4f9d\u8d56\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7b80\u5316\u7a0b\u5e8f\u7684\u590d\u6742\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function nextLevelItems(selectedItem) {<\/p>\n<p>    \/\/ \u6839\u636e\u9009\u4e2d\u7684\u9879\u8ba1\u7b97\u4e0b\u4e00\u7ea7\u7684\u53ef\u9009\u9879<\/p>\n<p>    \/\/ \u8fd9\u91cc\u7684\u5177\u4f53\u903b\u8f91\u53d6\u51b3\u4e8e\u5b9e\u9645\u60c5\u51b5<\/p>\n<p>    return [&#039;\u9009\u98792-1&#039;, &#039;\u9009\u98792-2&#039;];<\/p>\n<p>}<\/p>\n<p>function selectItem(levelIndex, selectedIndex) {<\/p>\n<p>    cascadeData[levelIndex].selected = selectedIndex;<\/p>\n<p>    if (cascadeData[levelIndex + 1]) {<\/p>\n<p>        updateNextLevel(levelIndex, selectedIndex);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u9ad8\u6548\u5904\u7406\u6570\u636e\u66f4\u65b0<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u7ea7\u8054\u7684\u7ea7\u522b\u591a\uff0c\u6bcf\u6b21\u66f4\u65b0\u65f6\u90fd\u4f1a\u91cd\u65b0\u8ba1\u7b97\u4e0b\u4e00\u7ea7\u7684\u5185\u5bb9\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002\u4e3a\u6b64\uff0c\u53ef\u4ee5\u5c06\u5df2\u7ecf\u8ba1\u7b97\u8fc7\u7684\u7ed3\u679c\u7f13\u5b58\u4ee5\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let cache = {};<\/p>\n<p>function setCascadingCache(level, selectedItem, nextItems) {<\/p>\n<p>    let cacheKey = `${level}-${selectedItem}`;<\/p>\n<p>    cache[cacheKey] = nextItems;<\/p>\n<p>}<\/p>\n<p>function getCascadingCache(level, selectedItem) {<\/p>\n<p>    let cacheKey = `${level}-${selectedItem}`;<\/p>\n<p>    return cache[cacheKey];<\/p>\n<p>}<\/p>\n<p>function updateNextLevelWithCache(currentLevel, selectedIndex) {<\/p>\n<p>    let cacheItems = getCascadingCache(currentLevel, selectedIndex);<\/p>\n<p>    if (!cacheItems) {<\/p>\n<p>        cacheItems = nextLevelItems(cascadeData[currentLevel].items[selectedIndex]);<\/p>\n<p>        setCascadingCache(currentLevel, selectedIndex, cacheItems);<\/p>\n<p>    }<\/p>\n<p>    \/\/ \u7701\u7565\u548c\u4e0a\u8ff0\u9012\u5f52\u66f4\u65b0\u4ee3\u7801\u76f8\u540c\u7684\u90e8\u5206...<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6700\u7ec8\u7ea7\u8054\u7ed3\u679c\u7684\u83b7\u53d6<\/h3>\n<\/p>\n<p><p>\u7ea7\u8054\u7b97\u6cd5\u6700\u7ec8\u9700\u8981\u83b7\u53d6\u7528\u6237\u5728\u6bcf\u4e00\u7ea7\u522b\u4e0a\u7684\u9009\u62e9\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u904d\u5386\u6570\u636e\u7ed3\u6784\u6765\u6784\u9020\u6700\u7ec8\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function getCascadeResult() {<\/p>\n<p>    return cascadeData.map(levelData =&gt; {<\/p>\n<p>        return levelData.items[levelData.selected];<\/p>\n<p>    });<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7ea7\u8054\u7b97\u6cd5\u5728JavaScript\u4e2d\u7684\u5173\u952e\u662f\u5229\u7528\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u6765\u7ba1\u7406\u72b6\u6001\uff0c\u5e76\u901a\u8fc7\u4e8b\u4ef6\u9a71\u52a8\u7684\u7528\u6237\u4ea4\u4e92\u6765\u66f4\u65b0\u8fd9\u4e9b\u72b6\u6001\u3002\u5728\u7f16\u7801\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6570\u636e\u548c\u72b6\u6001\u7684\u6d41\u5411\u4ee5\u53ca\u66f4\u65b0\u6548\u7387\uff0c\u786e\u4fdd\u7b97\u6cd5\u7684\u6027\u80fd\u548c\u51c6\u786e\u6027\u3002\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u57fa\u672c\u4f46\u5b8c\u6574\u7684\u7ea7\u8054\u7b97\u6cd5\uff0c\u5b83\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u6269\u5c55\u548c\u5b9a\u5236\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u4ec0\u4e48\u662f\u7ea7\u8054\u7b97\u6cd5\u5728JavaScript\u4e2d\u7684\u5e94\u7528\u573a\u666f\uff1f<\/strong><\/p>\n<p>\u7ea7\u8054\u7b97\u6cd5\u5728JavaScript\u4e2d\u5e38\u7528\u4e8e\u5904\u7406\u5177\u6709\u5173\u8054\u5173\u7cfb\u7684\u6570\u636e\uff0c\u4f8b\u5982\u7701\u5e02\u533a\u9009\u62e9\u3001\u591a\u7ea7\u5206\u7c7b\u7b5b\u9009\u7b49\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5b9e\u73b0\u4e00\u4e2a\u7ea7\u8054\u7b97\u6cd5\uff1f<\/strong><\/p>\n<p>\u5b9e\u73b0\u4e00\u4e2a\u7ea7\u8054\u7b97\u6cd5\u7684\u5173\u952e\u662f\u8981\u7406\u89e3\u6570\u636e\u7684\u7ed3\u6784\u548c\u5173\u7cfb\uff0c\u5e76\u7f16\u5199\u76f8\u5e94\u7684\u4ee3\u7801\u903b\u8f91\u3002\u9996\u5148\uff0c\u9700\u8981\u5b9a\u4e49\u597d\u6570\u636e\u7684\u7ed3\u6784\uff0c\u4f8b\u5982\u4f7f\u7528\u5bf9\u8c61\u6216\u6570\u7ec4\u6765\u8868\u793a\u4e0d\u540c\u5c42\u7ea7\u7684\u6570\u636e\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e8b\u4ef6\u76d1\u542c\u5668\u6765\u76d1\u542c\u7528\u6237\u7684\u9009\u62e9\u64cd\u4f5c\uff0c\u6839\u636e\u7528\u6237\u9009\u62e9\u7684\u5185\u5bb9\uff0c\u52a8\u6001\u66f4\u65b0\u4e0b\u4e00\u7ea7\u7684\u9009\u9879\uff0c\u5b9e\u73b0\u7ea7\u8054\u6548\u679c\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u73b0\u6210\u7684\u7ea7\u8054\u63d2\u4ef6\u6216\u5e93\u53ef\u4ee5\u4f7f\u7528\uff1f<\/strong><\/p>\n<p>\u662f\u7684\uff0c\u6709\u5f88\u591a\u5f00\u6e90\u7684\u63d2\u4ef6\u6216\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5b9e\u73b0\u7ea7\u8054\u6548\u679c\uff0c\u4f8b\u5982jQuery\u3001Vue.js\u548cReact\u7b49\u3002\u8fd9\u4e9b\u5de5\u5177\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ec4\u4ef6\u548cAPI\uff0c\u53ef\u4ee5\u51cf\u5c11\u4f60\u7684\u5f00\u53d1\u5de5\u4f5c\u91cf\uff0c\u5e76\u4e14\u5177\u6709\u826f\u597d\u7684\u517c\u5bb9\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9879\u76ee\u9700\u6c42\u9009\u62e9\u6700\u9002\u5408\u7684\u5de5\u5177\u6765\u5b9e\u73b0\u7ea7\u8054\u7b97\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u7ea7\u8054\u7b97\u6cd5\u901a\u5e38\u6d89\u53ca\u5230\u5c06\u8f93\u5165\u5206\u6210\u591a\u7ea7\u5904\u7406\u3001\u5904\u7406\u5b50\u7ea7\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u3001\u4ee5\u53ca\u6700\u540e\u7efc\u5408\u5404\u4e2a\u7ea7 [&hellip;]","protected":false},"author":3,"featured_media":170295,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/170292"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=170292"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/170292\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/170295"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=170292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=170292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=170292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}