I am trying to do something may be easy for some of you, but I spend hours to do it.
I have a form with checkbox as the following
<form class="form-style" id="st-21">
<label style="margin-right:10px">
<input type="checkbox" data-type="2" data-sensor="1" data-family="2" name="b1" style="margin-right:10px" checked="checked">
Température bas(C)
</label>
<label style="margin-right:10px">
<input type="checkbox" data-type="2" data-sensor="4" data-family="2" name="b4" style="margin-right:10px" checked="checked">
Température haut(C)
</label>
<label style="margin-right:10px">
<input type="checkbox" data-type="5" data-sensor="21" data-family="4" name="ba" style="margin-right:10px" checked="checked">
Batterie(V)
</label>
<label style="margin-right:10px">
<input type="checkbox" data-type="6" data-sensor="18" data-family="5" name="an" style="margin-right:10px">
Vitesse du vent(km/h)
</label>
<label style="margin-right:10px">
<input type="checkbox" data-type="7" data-sensor="19" data-family="5" name="wd" style="margin-right:10px">
Direction du vent(Degré)
</label>
<label style="margin-right:10px">
<input type="checkbox" data-type="8" data-sensor="20" data-family="6" name="wd" style="margin-right:10px">
Radiation solaire (W/m2)
</label>
</form>
Only two of them can be selected. I check a third checkbox, it must unselect the next checked checkbox
I do not know if it's a good start, but I would start with
$(".form-style").on('change',function() {
var sList=[];
var i = 0;
$(this).find('input:checkbox').each(function () {
if($(this).is(':checked'))
{
sList[i] = parseInt($(this).data("type"));
}
});
});
If look at all checkbox in my form, and add an index in sList array, when it found a checkbox checked.
If I check a new checkbox, Q1: is there way to detect which checkbox has been checked.
It look at the checkboxes, and as I checked a new checkbox, I added the following
$(".form-style").on('change',function() {
var sList=[];
var i = 0;
$(this).find('input:checkbox').each(function () {
if($(this).is(':checked'))
{
if(sList.length > 1){
// HOW CAN I REMOVE THE NEXT CHECKED CHECKBOX
}
sList[i] = parseInt($(this).data("type"));
}
});
});
But from that point, I need to remove the next checkbox which has been checked. in order to have always two checked.
Could help me to find the next checked checkbox, or tell me how can I make it matter
Many thanks
Aucun commentaire:
Enregistrer un commentaire