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>
3

4 Answers 4

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>

Sign up to request clarification or add additional context in comments.

Comments

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>

2 Comments

@EverythingWithUniverse If you run my example you can see that it returns the selected value. Please clarify what is the problem
i copied and pasted, but when i select lab 02, result same as lab 01
0

You can use in simple way like:

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

May this help for you.

Comments

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>

7 Comments

same result occured
which result you want to get? @EverythingWithUniverse
i want to retrieve data from database as follows, when i select lab 01, should be show computers belongs lab one and when select lab 02 should be show computers belongs to lab two. but both time shows only lab 01 computers
try like this <option value="Lab 02">Lab-02</option>, don't add space in values
Alert working, but query is not working, query work only for lab 01, when select lab 02 result same as lab 01
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.