我有存盤價值和兩組資料:
$selectedColor
const productColor = {
options: [
{id:0, title: 'white'},
{id:1, title: 'black'},
{id:2, title: 'red'},
{id:3, title: 'yellow'}
],
}
const productType = {
options: [
{id:0, title: 'somethings', hiddenInColor: 'white'},
{id:1, title: 'somethings', hiddenInColor: 'black'},
{
id:2,
title: 'somethings',
hiddenInColor:
//here should be black AND white
//problem is I can't understand how to use nested json with disable attribute (see below)
}
],
}
然后我填充了產品型別和顏色的輸入(型別:收音機)
{#each productType.options as option (option.id)}
{#if activeTab !== option.hiddenIn}
<li>
<label>
<input type="radio" name="name"
value={option.id}
on:change={() => optionHasChanged(option.name, somethings)}
disabled={option.hiddenInColor == $selectedColor}
bind:group={defaultProductType}>
{option.title}
</label>
</li>
{/if}
{/each}
我試圖 -> 禁用輸入 IF $selectedColor == hiddenInColor
我是這樣做的 ->
disabled={option.hiddenInColor == $selectedColor}
它運作良好。但是...僅當您針對 1 個值對其進行測驗時
我無法理解如何一次檢查超過 1 種顏色。所以基本上,使用我的 const productType -> 如果 $selectedColor == 'black' || 我想禁用 id:3 '白色的'
有什么建議嗎?或者也許有更緊湊的解決方案?謝謝
uj5u.com熱心網友回復:
實作此目的的最簡單方法是使用陣列hiddenInColors而不是單個標量值:
const productType = {
options: [
{id:0, title: 'somethings', hiddenInColors: ['white']},
{id:1, title: 'somethings', hiddenInColors: ['black']},
{id:2, title: 'somethings', hiddenInColors: ['black','white']},
],
}
然后像這樣設定禁用選項:
disabled={option.hiddenInColors.includes($selectedColor)}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/394933.html
