[英]Disable select option based on JSON data
我使用Jiren過濾器庫來過濾JSON數據。 因此,當我使用某些過濾條件時,我想在select
禁用未使用的選項。
例如:在我的結果中,我沒有任何巡航類型“ Silversea Expedition”,我希望在過濾器中禁用此選項。
這是我的代碼:
HTML:
<div class="well">
<fieldset id="cruiseType_criteria">
<legend>Cruise Type</legend>
<select class="form-control" id="type_filter" role="listbox">
<option value="all">All</option>
<option value="Silversea Classic">Cruise</option>
<option value="Silversea Expedition">Expedition</option>
</select>
</fieldset>
</div>
JSON對象:
{
"id": 23,
"Destination": "Mediterranean",
"DestinationID": "263960e8-9eaa-4ab0-a040-25bdf94479ee",
"DepartureDate": "2016-02-09T04:45:01 -01:00",
"Ship": "Explorer",
"ShipID": "8b7571a9-1a73-40c3-916f-27b3981167d1",
"CruiseType": "Silversea Classic",
"Duration": 13,
"AllInclusive": true,
"WaitList": false,
"FareFrom": 5434,
"Currency": "$",
"Cities": [
"ad",
"magna",
"dolore",
"quis",
"magna",
"sint",
"consequat",
"nostrud",
"amet",
"fugiat",
"proident",
"aliqua"
],
"VoyageCode": "{{integer(100, 9999)}",
"Features": [
{
"id": 0,
"name": "Diving"
},
{
"id": 1,
"name": "Wellness"
}
],
"url": "silversea.com"
},
先感謝您,
考慮到您的json數據在jsonObj
而CruiseType
是一個數組。
var cruiseType= jsonObj.cruseType;
$("#type_filter").each(function()
{
// checking if option is present in cruiseType array or not
if(jQuery.inArray($(this).val(), cruiseType) == -1){
$(this).prop('disabled', true);
}
});
上面的代碼將添加disbled=true
,其不存在於所述選項CruiseType
從jsonObj
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.