-2

I tried to get the ID on the option selected when the modal was in show state.

$("#modal-form").on('shown.bs.modal', function(e) {
  console.log("CHANGE");
  var id = $("#customer option:selected").val();
  console.log(id);
});
<div class="form-group">
  <label for="customer" class="col-md-2 control-label">Customer</label>
  <div class="col-md-9">
    <select class="form-control" id="customer" name="customer" class="form-control">
      <option value="John">John</option>
      <option value="Alex">Alex</option>
    </select>
    <span class="help-block with-errors"> </span>
  </div>
</div>
halfer
  • 19,824
  • 17
  • 99
  • 186
Mack Zohn
  • 11
  • 6

3 Answers3

0

Try the following code. Find the element from the parent,

$(document).on('shown.bs.modal', '#modal-form', function() {
  console.log("CHANGE");
  //var id = $("#customer>option:selected").val();
  var id = $('#customer').find(":selected").val();
  alert(id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
  <label for="customer" class="col-md-2 control-label">Customer</label>
  <div class="col-md-9">
  <form id="modal-form">
    <select class="form-control" id="customer" name="customer" class="form-control">
      <option value="John">John</option>
      <option value="Alex">Alex</option>
    </select>
    </form>
    <span class="help-block with-errors" </span>
  </div>
</div>
Ramesh
  • 2,297
  • 2
  • 20
  • 42
  • @indradana If you are using the same value for `value` and `text`, possibly you can use `text()`. like this `var id = $('#customer').find(":selected").text();`.This is the best way so far I've known – Ramesh Aug 19 '18 at 04:38
0

Hope this will be helpful.

$("#customer").change(function(e) {
  alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
  <label for="customer" class="col-md-2 control-label">Customer</label>
  <div class="col-md-9">
    <select class="form-control" id="customer" name="customer" class="form-control">
      <option value="John">John</option>
      <option value="Alex">Alex</option>
    </select>
    <span class="help-block with-errors" </span>
  </div>
</div>
Anshuman
  • 758
  • 7
  • 23
0

I find out you have not selected any option and you are checking for select option, select any one option and check it out.

$("#modal-form").on('shown.bs.modal', function(e) {
  console.log("CHANGE");
  var id = $("#customer option:selected").val();
  console.log(id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
  <label for="customer" class="col-md-2 control-label">Customer</label>
  <div class="col-md-9">
    <select class="form-control" id="customer" name="customer" class="form-control">
      <option value="John" selected>John</option>
      <option value="Alex">Alex</option>
    </select>
    <span class="help-block with-errors"> </span>
  </div>
</div>
Ramesh
  • 2,297
  • 2
  • 20
  • 42
jvk
  • 2,133
  • 3
  • 19
  • 28