Так как вы хотите посчитать опцию, если выбранное значение 1 (необязательно) , необходимо выполнить подсчет события change
выбора select .
Также я считаю, что вы хотите показать элемент, когда if(count > 0){
, а не когда if(count >= 0){
.
. Вы можете попробовать следующий способ:
$(document).ready(function () {
var el = $('#optional_subject_number');
var count = 0;
$('.selectpicker').change(function(){
$(this).val() == '1'? count++ : count--;
if(count > 0){
$(el).text('Total Optional Subject Per Students ' + count);
$(el).show();
}
else{
$(el).hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<table class="table table-hover table-striped" id="subject_tb">
<thead>
<tr>
<th width="30%">Subject Name</th>
<th width="20%" class="text-center"><span style="margin-left:-64px">Choice</span>
</th>
</tr>
</thead>
<tbody id="subject_table">
<tr>
<td>Bangla</td>
<td class="td-actions text-right">
<select name="select_mandatory"
class="selectpicker select_mandatory"
data-style="btn-default btn-block btn-outline"
data-menu-style="dropdown-blue" required>
<option value="0" class="mandatory">Mandatory
</option>
<option value="1" class="optional">Optional
</option>
</select>
</td>
</tr>
<tr>
<td>English</td>
<td class="td-actions text-right">
<select name="select_mandatory"class="selectpicker select_mandatory"
data-style="btn-default btn-block btn-outline" data-menu
style="dropdown-blue" required>
<option value="0" class="mandatory">Mandatory</option>
<option value="1" class="optional">Optional
</option>
</select>
</td>
</tr>
</tbody>
</table>
<div class="row" id="optional_subject_number">
<label class="col-sm-8 col-form-label">
</label>
<div class="col-sm-4">
<div class="form-group">
<input class="form-control" type="number"
name="optional_subject" value="" number="true"
id="optional_subject" style="margin-left: -100% !important;"/>
</div>
</div>
</div>
</html>