gpt4 book ai didi

javascript - 使用 JavaScript 创建带有对象数组的三级依赖下拉列表

转载 作者:行者123 更新时间:2023-12-03 01:39:24 25 4
gpt4 key购买 nike

我正在从 API(动态数据)获取的对象数组中创建三级下拉列表。我尝试从 ssmlGender(语音性别) 字段中删除重复值,并尝试了不同的方法。但是,无法获取正确的数据。

在第一个下拉列表中,我显示性别:“ssmlGender”字段中的“女性”和“男性”。当用户在第一个下拉列表中选择男性或女性时,第二个下拉列表必须填充可用于特定性别的“语言代码”。选择语言代码后,应显示具有所选性别和语言代码的“名称”字段。

示例数组:

0: {languageCodes: Array(1), name: "es-ES-Standard-A", ssmlGender: "FEMALE", naturalSampleRateHertz: 24000}
1:
{languageCodes: Array(1), name: "it-IT-Standard-A", ssmlGender: "FEMALE", naturalSampleRateHertz: 24000}
2:
{languageCodes: Array(1), name: "ja-JP-Standard-A", ssmlGender: "FEMALE", naturalSampleRateHertz: 22050}
3:
{languageCodes: Array(1), name: "ko-KR-Standard-A", ssmlGender: "FEMALE", naturalSampleRateHertz: 22050}
8:{languageCodes: Array(1), name: "en-US-Wavenet-D", ssmlGender: "MALE", naturalSampleRateHertz: 24000}
9:
{languageCodes: Array(1), name: "en-US-Wavenet-A", ssmlGender: "MALE", naturalSampleRateHertz: 24000}
10:
{languageCodes: Array(1), name: "en-US-Wavenet-B", ssmlGender: "MALE", naturalSampleRateHertz: 24000}
11:
{languageCodes: Array(1), name: "en-US-Wavenet-C", ssmlGender: "FEMALE", naturalSampleRateHertz: 24000}
12:
{languageCodes: Array(1), name: "en-US-Wavenet-E", ssmlGender: "FEMALE", naturalSampleRateHertz: 24000}
13:
{languageCodes: Array(1), name: "en-US-Wavenet-F", ssmlGender: "FEMALE", naturalSampleRateHertz: 24000}
14:
{languageCodes: Array(1), name: "en-GB-Standard-A", ssmlGender: "FEMALE", naturalSampleRateHertz: 24000}
15:
{languageCodes: Array(1), name: "en-GB-Standard-B", ssmlGender: "MALE", naturalSampleRateHertz: 24000}

当我们单击特定数组时,它将显示此链接中给出的数据:/image/BFYGm.jpg

我的 HTML:

 <div class="row">
<label>Voice Gender</label>
<div class="input-field col s12">
<select id="gender">
<option value="" selected>Choose your option</option>
</select>
</div>
</div>

<div class="row">
<label>Voice Language</label>
<div class="input-field col s12">
<select id="languages">
<option value="" selected>Choose your languages</option>
</select>
</div>
</div>

<div class="row">
<label>Voice Name</label>
<div class="input-field col s12">
<select id="entries">
<option value="" selected>Choose your option</option>
</select>
</div>
</div>

我的 JavaScript:

for(var i=0;i<response.result.voices.length;i++){
var values= response.result.voices;

//To remove the duplicate value of "Male" and "Female" iteration and to display only once.

var valueArr = values.map(function(item){ return item.ssmlGender });
var isDuplicate = valueArr.some(function(item, idx){
return valueArr.indexOf(item) != idx
});
console.log(isDuplicate);
$('#entries').append('<option value='+(i+1)+'>' + response.result.voices[i].name+ '</option>');


//Added custom options of "MALE" AND "FEMALE" directly into the voice gender field of HTML to remove the duplicate values.But, it didn't work out.

/*if(document.getElementById("gender").value=="FEMALE"){
if(response.result.voices[i].ssmlGender=="FEMALE")
{
finalLanguages=response.result.voices[i].languageCodes;
}
}
if(document.getElementById("gender").value =="MALE"){
if(response.result.voices[i].ssmlGender=="MALE")
{
finalLanguages=response.result.voices[i].languageCodes;
}
}
$("#languages").append('<option value=' + (i+1)+ '>'+finalLanguages+ '</option>');*/
}

非常感谢。

最佳答案

let languageCodes = [
{ languageCodes: ['123'], name: 'es-ES-Standard-A', ssmlGender: 'FEMALE', naturalSampleRateHertz: 2403},
{languageCodes: ['214'], name: 'it-IT-Standard-A', ssmlGender: 'MALE', naturalSampleRateHertz: 21000},
{languageCodes: ['147'], name: 'ja-JP-Standard-A', ssmlGender: 'FEMALE', naturalSampleRateHertz: 3200},
{languageCodes: ['290'], name: 'ko-KR-Standard-A', ssmlGender: 'MALE', naturalSampleRateHertz: 2330},
{languageCodes: ['173'], name: 'en-US-Wavenet-B', ssmlGender: 'FEMALE', naturalSampleRateHertz: 412},
{languageCodes: ['583'], name: 'en-GB-Standard-A', ssmlGender: 'MALE', naturalSampleRateHertz: 8493}
];

// Register Change listeners
function registerEventListeners() {
document.getElementById('gender').addEventListener('change', changeVoiceLanguageOption, false);
document.getElementById('languages').addEventListener('change', changeVoiceNameOption, false);
}

function changeVoiceNameOption() {
const valueSelected = document.getElementById('languages').value;
let entriesElement = document.getElementById('entries');
entriesElement.innerHTML = '';
entriesElement.innerHTML += '<option value="" selected>Choose your option</option>'

languageCodes.filter(( item ) => {
return item.languageCodes.includes(valueSelected);
}).forEach(( item ) => {
entriesElement.innerHTML += `<option value="${item.name}"> ${item.name} </option>`
})
}

function changeVoiceLanguageOption() {
const valueSelected = document.getElementById('gender').value;
let languageElement = document.getElementById('languages');
languageElement.innerHTML = ''; // This is what prevents duplication
languageElement.innerHTML += '<option value="" selected>Choose your languages</option>'

languageCodes.filter(( item ) => {
return item.ssmlGender === valueSelected;
}).forEach(( filteredItems ) => {
filteredItems.languageCodes.forEach(( languageCode ) => {
languageElement.innerHTML += `<option value="${languageCode}"> ${languageCode} </option>`
})
})


}

function populateGender() {

let genderElement = document.getElementById('gender');
let genderObject = {};
languageCodes.forEach(( item ) => {
genderObject[item.ssmlGender] = '';
})
Object.keys(genderObject).forEach(( item ) => {
genderElement.innerHTML += `<option value="${item}"> ${item} </option>`
})
}

registerEventListeners();
populateGender();
<div class="row">
<label>Voice Gender</label>
<div class="input-field col s12">
<select id="gender">
<option value="" selected>Choose your option</option>
</select>
</div>
</div>

<div class="row">
<label>Voice Language</label>
<div class="input-field col s12">
<select id="languages">
<option value="" selected>Choose your languages</option>
</select>
</div>
</div>

<div class="row">
<label>Voice Name</label>
<div class="input-field col s12">
<select id="entries">
<option value="" selected>Choose your option</option>
</select>
</div>
</div>

关于javascript - 使用 JavaScript 创建带有对象数组的三级依赖下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50928375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com