I’m using the following code to enable/disable radio buttons according to the radio button value which I clicked.
$(document).ready(function(){
//array
var data = [
{"fruit": "apple", "quantity": "5", "package": "bag"},
{"fruit": "apple", "quantity": "10", "package": "bag"},
{"fruit": "apple", "quantity": "15", "package": "bag"},
{"fruit": "grapes", "quantity": "10", "package": "box"},
{"fruit": "mango", "quantity": "5", "package": "bag"},
{"fruit": "mango", "quantity": "10", "package": "bag"},
{"fruit": "mango", "quantity": "15", "package": "box"},
{"fruit": "mango", "quantity": "20", "package": "box"},
{"fruit": "pineapple", "quantity": "5", "package": "bag"},
{"fruit": "pineapple", "quantity": "10", "package": "bag"},
{"fruit": "pineapple", "quantity": "15", "package": "box"},
{"fruit": "pineapple", "quantity": "20", "package": "box"}
];
function info(cat,value){
var filteredValue = data.filter(function (item) {
return item[cat]==value;
});
var vf = filteredValue;
for(var i=0; i<vf.length;i++){
var obj = vf[i];
for(var key2 in obj){
if(key2!=cat){
//console.log(key2);
var obj2 = obj[key2];
//console.log(obj2);
$("input[name='opt_"+key2+"'][value!='"+obj2+"']").prop("disabled","disabled");
}
}
}
}
$(".info input[type='radio']").click(function(){
var value = $(this).val();
var cat = $(this).attr("data-cat");
info(cat,value);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container info">
<div class="row">
<div class="col-md-12">
<div>Fruit</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" data-cat="fruit" name="opt_fruit" value="apple">Apple
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" data-cat="fruit" name="opt_fruit" value="grapes">Grapes
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" data-cat="fruit" name="opt_fruit" value="mango">Mango
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" data-cat="fruit" name="opt_fruit" value="pineapple">Pineapple
</label>
</div>
</div>
<div class="col-md-12">
<div>Quantity</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" data-cat="quantity" name="opt_quantity" value="5">5
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" data-cat="quantity" name="opt_quantity" value="10">10
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" data-cat="quantity" name="opt_quantity" value="15">15
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" data-cat="quantity" name="opt_quantity" value="20">20
</label>
</div>
</div>
<div class="col-md-12">
<div>Package</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" data-cat="package" name="opt_package" value="bag">Bag
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" data-cat="package" name="opt_package" value="box">Box
</label>
</div>
</div>
</div>
</div>
As you can see, the problem is when I click a radio button input, the others turn disabled incorrectly:
And I want to get this result, for example, if I click on "apple", it must enable only quantity and package radio buttons available for that value according to data array like this:
How can I fix it? I’d like to receive your help.
2
Answers
In this loop, each data in obj make other inputs which have different value disabled.
For example, when you click ‘apple’, obj will be like below.
{fruit: 'apple', quantity: '5', package: 'bag'}
{fruit: 'apple', quantity: '10', package: 'bag'}
{fruit: 'apple', quantity: '15', package: 'bag'}
And in the for loop,
quantity: '5'
makesinput[name='opt_quantity']
which has value ’10’, ’15’, ’20’ disabledquantity: '10'
makesinput[name='opt_quantity']
which has value ‘5’, ’15’, ’20’ disabledquantity: '15'
makesinput[name='opt_quantity']
which has value ‘5’, ’10’, ’20’ disabledAs a result, all
input[name='opt_quantity']
disabled.You can simply disabled all input first, and then change "disable" prop false according to the clicked radio button value.
*Additionally, it seems to need multiple conditions filtering data when 2 or more category( if you make more than 3 category ) have value.
Your requirements required involving multiple steps to achieve this.
Start with defining which properties will included in the filter:
Add a click event to detect user selection:
Every time when user select an option, based on your properties defined above, loop through each property and get their current selected value:
This will return a filter data object:
Filter the fruits data by using the filter data object. Following function will loop through each item and compare every filter property:
Next let’s get the available options (enabled radio buttons) array from the filtered results by using this method:
The returned available options array will looks like this:
Finally update the radio buttons status using the available options array:
Demo: