javascript - 获取第二个选择框的值以获取结果
问题描述
我正在尝试在我的项目的嵌套选择中获取城市、县、区域。所以我有 3 级嵌套选择。但是当我尝试使用 2 个变量获取数据时,我无法将“first selected select”的值传递给 fetch url。
如何将首先选择的选择值的值传递给 fetch url?
我需要用值替换this.MUSTBEVALUEOFTHESECNDSELECTEDSELECT
这是一个演示 http://jsfiddle.net/krsj3ecy/
<script src="https://unpkg.com/vue"></script>
<script type="text/javascript">
window.addEventListener('load', function() {
var app = new Vue({
el: '.app',
name: "IlIlceUygulaması",
data: {
iller: {},
ilceler: {},
mahalleler: {},
selected: 0,
ilSecildi: false,
ilceSecildi: false,
},
methods: {
illeriGetir() {
fetch("https://www.example.com/loc/")
.then(result => result.json())
.then(result => {
this.iller = result;
})
},
ilceleriGetir() {
this.ilSecildi = true;
fetch(`https://www.example.com/loc/${this.selected}`)
.then(result => result.json())
.then(result => {
this.ilceler = result;
})
},
mahalleleriGetir() {
this.ilSecildi = true;
this.ilceSecildi = true;
fetch(`https://www.example.com/loc/${this.MUSTBEVALUEOFTHESECONDSELECTEDSELECT}/${this.selected}`)
.then(result => result.json())
.then(result => {
this.mahalleler = result;
})
}
}
})
app.illeriGetir();
});
</script>
<div class="app" style="margin-top:50px">
<b>İl Seçiniz</b>
<select class="form-control col-md-3" v-model="selected" v-on:change="ilceleriGetir()">
<option v-for="list in iller"
v-bind:value="list.cityid">
{{list.cityname}}
</option>
</select>
<div v-if="ilSecildi" style="margin-top:50px">
<b> İlçe Seçiniz</b>
<select class="form-control col-md-3" v-on:change="mahalleleriGetir()">
<option v-for="list in ilceler"
v-bind:value="list.countyid">
{{list.countyname}}
</option>
</select>
</div>
<div v-if="ilceSecildi" style="margin-top:50px">
<b> Mahalle Seçiniz</b>
<select class="form-control col-md-3">
<option v-for="list in mahalleler"
v-bind:value="list.areaid">
{{list.areaname}}
</option>
</select>
</div>
</div>
解决方案
创建一个新对象,该对象具有每个选择框的属性以进行变异:
selections: {
iller: null,
ilceler: null,
mahalleler: null
}
更新所有v-model
绑定:
v-model="selections.iller"
v-model="selections.ilceler"
v-model="selections.mhalleler"
现在您可以正确获取它们。
推荐阅读
- android - 任务 whenAllSuccess 返回 Uri 而不是 List
- java - 将 JSON 与记录中的列表绑定
- python - 如何从json流中解析对象
- excel - 如何更改此脚本对数据进行分组的方向?
- maven - 在 Karaf 容器中打包和部署 OSGi 包和 Karaf 功能
- javascript - Gmail 拒绝呈现图像背景 url 属性
- java - 分离实体传递给坚持:不允许我保存对象
- python - 创建一个字典,名字作为键,与键关联的全名作为值
- android - 在明确删除之前,SingleValueEvent 侦听器是否计入 Firebase RealTime DB 中的“同时连接”?
- php - Laravel 5.6 - 禁用错误