0

I have dropdown list and i want to retrieve data from database when select value of downlist. This is working without having error. But this is working when i click on dropdown list, not in dropdown value. That mean work only for default value. please help me how to rectify this error. code as below.

HTML code for Dropdown list

<select name="lab_no" id="lab-no" class="form-control">
    <option value="Lab 01" >Lab 01</option>
    <option value="Lab 02">Lab 02</option>
</select> 

Jquery Code is Here

<script type="text/javascript">
    $(document).ready(function () {
        $("option").click(function () {
            var txt = $("#lab-no:selected").val();
            if (txt = '') {

            } else {
                $('#table-row').html('');
                $.ajax({
                    url: "../svr/com-list.php",
                    method: "post",
                    data: {search: txt},
                    dataType: "text",
                    success: function (data) {
                        $('#table-row').html(data);
                    }
                });
            }
        });
    });
</script>
Nino
  • 6,931
  • 2
  • 27
  • 42
serverAdmin123
  • 115
  • 1
  • 13

4 Answers4

1

First you need to target your select $('#lab-no') and use change event instead of click. Then you can target the selected option.

  $(document).ready(function () {
    $("#lab-no").change(function () {
    var txt = $("select option:selected").val()
    console.log(txt)
    if (txt = '') {
    
    } else {
      $('#table-row').html('');
      $.ajax({
        url: "../svr/com-list.php",
        method: "post",
        data: {search: txt},
        dataType: "text",
        success: function (data) {
          $('#table-row').html(data);
        }
      });
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="lab_no" id="lab-no" class="form-control">
  <option value="Lab 01" >Lab 01</option>
  <option value="Lab 02">Lab 02</option>
</select>
Andrew
  • 449
  • 3
  • 7
0

You should try like this

1.You need to change jquery event

    <script type="text/javascript">
       $(document).ready(function () {
        $("#lab-no").change(function () {
          var txt = $("#lab-no").val();
             alert(txt) //place alert and check value. You will get values which you have selected
             if (txt == '') { //make changes here

             } else {
               $('#table-row').html('');
                 $.ajax({
                  url: "../svr/com-list.php",
                   method: "post",
                   data: {search: txt},
                   dataType: "text",
                   success: function (data) {
                      $('#table-row').html(data);
                    }
               });
           }
        });
   });
 </script>
Mayank Vadiya
  • 1,437
  • 2
  • 19
  • 32
0

Try with $("#lab-no").change. This Way your event will trigger after you have made a change in the dropdown.

You should not use :selected in $("#lab-no:selected").val() since its not needed. $("#lab-no").val() will return the selected value.

Working Demo

$(document).ready(function() {
  $("#lab-no").change(function() {
    var txt = $("#lab-no").val();
    console.log(txt)
    if (txt = '') {

    } else {
      $('#table-row').html('');
      $.ajax({
        url: "../svr/com-list.php",
        method: "post",
        data: {
          'search': txt
        },
        dataType: "text",
        success: function(data) {
          $('#table-row').html(data);
        }
      });
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="lab_no" id="lab-no" class="form-control">
    <option value="Lab 01">Lab 01</option>
    <option value="Lab 02">Lab 02</option>
</select>
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
0

You can use in simple way like:

var drpSelectedValue=$("#lab-no").val();

May this help for you.

Kunvar Singh
  • 1,779
  • 2
  • 13
  • 21