Niente da fare. Provato anche così, ma non crea le options.
codice:
<script>
// prettier-ignore
const initApp = async () => {
const brand = document.getElementById('brand')
const brandModel = document.getElementById('brand_model')
const objData = await fetch(<?= json_encode($brands) ?>).then(res =>res.json())
const fillBrandModel = ()=>{
const selectKey = brand.value
brandModel.textContent = ''
for (const key in objData) {
if (selectKey === key){
objData[key].forEach(text => {
const option = document.createElement('option')
option.textContent = text['Name']
brandModel.appendChild(option)
})
}
}
}
const fillBrand = (()=>{
Object.keys(objData).forEach(key => {
const option = document.createElement('option')
option.textContent = key
brand.appendChild(option)
})
fillBrandModel()
})()
brand.addEventListener("change",fillBrandModel)
}
document.addEventListener("DOMContentLoaded", initApp)
</script>
Nel codice sorgete l'array c'è
codice:
|
<script> |
|
// prettier-ignore |
|
const initApp = async () => { |
|
const brand = document.getElementById('brand') |
|
const brandModel = document.getElementById('brand_model') |
|
|
|
const objData = await fetch({"Acer":[{"ID":"4","Name":"Lorem iosum"}],"Alcatel":[{"ID":"5","Name":"Lorem iosum"}],"Apple":[{"ID":"2","Name":"Iphone 5"},{"ID":"12","Name":"Iphone 6"},{"ID":"13","Name":"Iphone 7"},{"ID":"14","Name":"Iphone 6 Plus"},{"ID":"15","Name":"Iphone 6S"},{"ID":"16","Name":"Iphone 6s Plus"}],"dasda":[{"ID":"6","Name":"Qualcosa"}],"Samsung":[{"ID":"1","Name":"Galaxy S10 Plus"},{"ID":"8","Name":"Galaxy S10"},{"ID":"9","Name":"Galaxy S10 Lite"},{"ID":"10","Name":"Galaxy S7 Edge"},{"ID":"11","Name":"Galaxy S8 plus"}],"Test":[{"ID":"7","Name":"Model"}],"Xiaomi":[{"ID":"3","Name":"Lorem iosum"}]}).then(res =>res.json()) |
|
|
|
const fillBrandModel = ()=>{ |
|
const selectKey = brand.value |
|
brandModel.textContent = '' |
|
for (const key in objData) { |
|
|
|
if (selectKey === key){ |
|
objData[key].forEach(text => { |
|
const option = document.createElement('option') |
|
option.textContent = text['Name'] |
|
brandModel.appendChild(option) |
|
}) |
|
} |
|
} |
|
} |
|
|
|
const fillBrand = (()=>{ |
|
|
|
Object.keys(objData).forEach(key => { |
|
const option = document.createElement('option') |
|
option.textContent = key |
|
brand.appendChild(option) |
|
}) |
|
fillBrandModel() |
|
})() |
|
|
|
brand.addEventListener("change",fillBrandModel) |
|
} |
|
|
|
document.addEventListener("DOMContentLoaded", initApp) |
|
</script> |
|
|