I have three select-option
with different values. What I need is that by subtracting the values add a class to the row.
This is my code (also available at JSFiddle )
$("tr").click(function() {
id = $(this).attr("id");
$("#" + id + ' ' + "select").each(function(cSelect) {
$(this).data('stored-value', $(this).val());
});
$("#" + id + ' ' + "select").change(function() {
var cSelected = $(this).val();
var cPrevious = $(this).data('stored-value');
$(this).data('stored-value', cSelected);
var otherSelects = $("#" + id + ' ' + "select").not(this);
otherSelects.find('option[value="' + this.value + '"]').removeAttr('disabled');
otherSelects.find('option[value="' + this.value + '"]').attr('disabled', 'disabled');
});
});
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<table id='example-table' class="table table-striped">
<thead>
<tr>
<th>UNO 1</th>
<th>DOS 2</th>
<th>TRES 3</th>
</tr>
</thead>
<tbody>
<tr id="1">
<td>
<select class="form-control" name="select1">
<option selected="true" disabled="disabled">Selecione 1º </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td>
<select class="form-control" name="select2">
<option selected="true" disabled="disabled">Selecione 2º </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td>
<select class="form-control" name="select3">
<option selected="true" disabled="disabled">Selecione 3º </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
</tr>
<tr id="2">
<td>
<select class="form-control" name="select1">
<option selected="true" disabled="disabled">Selecione 1º </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td>
<select class="form-control" name="select2">
<option selected="true" disabled="disabled">Selecione 2º </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td>
<select class="form-control" name="select3">
<option selected="true" disabled="disabled">Selecione 3º </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
</tr>
</tbody>
</table>
That's what the structure of HTML and JS is like. When selecting the three values you would need to subtract the values in this way:
valor_1 - valor_2 - valor_3
And depending on the result, give a class to the row:
- (value_1 - value_2 - value_3) = 0 addclass ('Good')
- (value_1 - value_2 - value_3) < 0 addclass ('Bad')
- (value_1 - value_2 - value_3) > 0 addclass ('Alert')
I hope you can help me.