如何实现根据选择不同的授权平台动态显示其对应的授权期限和价格?

聖光之護
发布: 2025-02-23 17:32:14
原创
469人浏览过

动态显示授权平台期限和价格

本文介绍如何根据选择的授权平台动态显示其对应的授权期限和价格。我们将使用javascript实现这一功能,并提供vue.js的示例代码。

如何实现根据选择不同的授权平台动态显示其对应的授权期限和价格?

数据结构:

我们假设后端返回的数据结构如下:

price_list: [
  {
    "title": "哔哩哔哩",
    "children": [
      {
        "id": 1,
        "platform": 1,
        "platform_title": "哔哩哔哩",
        "years": 0,
        "price": 0.01,
        "address": 1,
        "address_title": "一年",
        "show_title": "¥0.4元"
      },
      {
        "id": 19,
        "platform": 1,
        "platform_title": "哔哩哔哩",
        "years": 1,
        "price": 0.01,
        "address": 0,
        "address_title": "不限",
        "show_title": "¥1元"
      }
    ]
  },
  {
    "title": "微博",
    "children": [
      {
        "id": 17,
        "platform": 3,
        "platform_title": "微博",
        "years": 0,
        "price": 0.01,
        "address": 0,
        "address_title": "一年",
        "show_title": "¥0.2元"
      },
      {
        "id": 13,
        "platform": 3,
        "platform_title": "微博",
        "years": 0,
        "price": 0.01,
        "address": 0,
        "address_title": "五年",
        "show_title": "¥1元"
      }
    ]
  },
  // ...更多平台数据
]
登录后复制

JavaScript实现:

const priceList = [ /* ... 上述JSON数据 ... */ ];

const selectPlatform = document.getElementById("platformSelect");
const selectPeriod = document.getElementById("periodSelect");
const priceDisplay = document.getElementById("priceDisplay");

function updateOptions() {
  const selectedPlatform = selectPlatform.value;
  const platformData = priceList.find(p => p.platform === parseInt(selectedPlatform));

  selectPeriod.innerHTML = ''; // 清空之前的选项
  if (platformData) {
    platformData.children.forEach(period => {
      const option = document.createElement('option');
      option.value = period.address;
      option.text = period.address_title;
      selectPeriod.appendChild(option);
    });
    updatePrice();
  } else {
    priceDisplay.textContent = "请选择平台";
  }
}

function updatePrice() {
  const selectedPlatform = selectPlatform.value;
  const selectedPeriod = selectPeriod.value;
  const platformData = priceList.find(p => p.platform === parseInt(selectedPlatform));
  const periodData = platformData.children.find(p => p.address === parseInt(selectedPeriod));
  priceDisplay.textContent = periodData ? periodData.show_title : "请选择期限";
}


// 初始化平台选择框
priceList.forEach(platform => {
  const option = document.createElement('option');
  option.value = platform.platform;
  option.text = platform.title;
  selectPlatform.appendChild(option);
});

selectPlatform.addEventListener('change', updateOptions);
selectPeriod.addEventListener('change', updatePrice);

// 初始显示
updateOptions();
登录后复制

HTML结构 (示例):

<select id="platformSelect"></select>
<select id="periodSelect"></select>
<p>价格: <span id="priceDisplay"></span></p>
登录后复制

Vue.js实现 (简化版):

<template>
  <div>
    <select v-model="selectedPlatform" @change="updateOptions">
      <option v-for="platform in priceList" :key="platform.platform" :value="platform.platform">{{ platform.title }}</option>
    </select>
    <select v-model="selectedPeriod" @change="updatePrice">
      <option v-for="period in periodOptions" :key="period.address" :value="period.address">{{ period.address_title }}</option>
    </select>
    <p>价格: {{ price }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      priceList: [ /* ... 上述JSON数据 ... */ ],
      selectedPlatform: null,
      selectedPeriod: null,
      price: '',
      periodOptions: []
    };
  },
  computed: {
    platformData() {
      return this.priceList.find(p => p.platform === this.selectedPlatform);
    }
  },
  methods: {
    updateOptions() {
      this.periodOptions = this.platformData ? this.platformData.children : [];
      this.updatePrice();
    },
    updatePrice() {
      const periodData = this.periodOptions.find(p => p.address === this.selectedPeriod);
      this.price = periodData ? periodData.show_title : '';
    }
  },
  mounted() {
    if (this.priceList.length > 0) {
      this.selectedPlatform = this.priceList[0].platform;
      this.updateOptions();
    }
  }
};
</script>
登录后复制

记住替换/* ... 上述JSON数据 ... */为你的实际数据。 这两个示例都实现了根据平台选择动态更新期限选项和价格的功能。 Vue.js版本更简洁,并利用了数据绑定和响应式特性。 选择哪个版本取决于你的项目需求和技术栈。

以上就是如何实现根据选择不同的授权平台动态显示其对应的授权期限和价格?的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号