javascript - 2 Select with same options, alerting when it is already selected at the first Select -
i'm having hard time this, have 2 select same options. 2 select inputs named registrationdiscounttype1 , registrationdiscounttype2
so first choose discount registrationdiscounttype1 if add discount registrationdiscounttype2 if option selected in discount number 1 show error discount selected.
i've made javascript based on i've search far, can't make work, can me wrong code, or should use?
// change in discount 2 $("#registrationdiscounttype2").unbind('change').bind('change', function() if ($('#registrationdiscounttype2').closest('table').find('option[value=' + $('#registrationdiscounttype1').val() + ']:selected').length > 1) { alert('discount selected, please choose one'); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> <div class="form-group"> <label for="registrationdiscounttype1" class="col-sm-4 control-label">discount 1: </label> <div class="col-sm-8"> <select class="form-control" name="registrationdiscounttype1" id="registrationdiscounttype1"> <option value="">select discount </option> <?php foreach ($discounttypedata $discounttype) { ?> <option value="<?php echo $discounttype['discounttype_id']; ?>"> <?php echo $discounttype['discounttype_name']; ?> </option> <?php } ?> </select> </div> </div> <div class="form-group"> <label for="registrationdiscounttype2" class="col-sm-4 control-label">discount 2: </label> <div class="col-sm-8"> <select class="form-control" name="registrationdiscounttype2" id="registrationdiscounttype2"> <option value="">select discount </option> <?php foreach ($discounttypedata $discounttype) { ?> <option value="<?php echo $discounttype['discounttype_id']; ?>"> <?php echo $discounttype['discounttype_name']; ?> </option> <?php } ?> </select> </div> </div>
u can try
$(document).ready(function(){ $("#registrationdiscounttype2").unbind('change').bind('change', function() { if ($('#registrationdiscounttype2').val() == $('#registrationdiscounttype1').val()) { alert('discount selected, please choose one'); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="form-group"> <label for="registrationdiscounttype1" class="col-sm-4 control-label">discount 1: </label> <div class="col-sm-8"> <select class="form-control" name="registrationdiscounttype1" id="registrationdiscounttype1"> <option value="">select discount </option> <option value="1">1 </option> <option value="2">2 </option> </select> </div> </div> <div class="form-group"> <label for="registrationdiscounttype2" class="col-sm-4 control-label">discount 2: </label> <div class="col-sm-8"> <select class="form-control" name="registrationdiscounttype2" id="registrationdiscounttype2"> <option value="">select discount </option> <option value="1">1 </option> <option value="2">2 </option> </select> </div> <div class="form-group" id="result"> </div>
Comments
Post a Comment