1

I have a dropdown whose selected value is being fetched by this code:

$(document).on('change','#DropDown1',function()
 {
    var value1 = $(this).children("option:selected").val();
    var data = {"key": value1};

     $.ajax({
        url : "value_choices",
        type: "GET",
        data: data,
        dataType: "json"
 });

Now i have two dropdowns instead of one where id of second dropdown is DropDown2. How can i fetch values of both the dropdowns? What i have searched till now, I thought that i should use an approach like this:

 $(document).on('change','#DropDown1, #DropDown2',function()
 {
    var value1 ,value2= $(this).children("option:selected,option:selected").val(); 

     //I know that the above written line of code is absolutely wrong. I need your help in this.
    //Moreover please check whether rest of the code is right or not.

    var data = {"key": value1,"color":value2};
     $.ajax({
        url : "value_choices",
        type: "GET",
        data: data,
        dataType: "json"
 });

Update This is my actual code what i am using in my application.

<script type="text/javascript">

$(document).on('change','#keyDropDown, #valueOfKey',function()
 {
    //var chosenKey = $(this).children("option:selected").val();
    var chosenKey = $('#keyDropDown').val();
    var chosenValue = $('#valueOfKey').val();
    var data = {"key": chosenKey,"value":chosenValue};

    $.ajax({
        url : "value_choices",
        type: "GET",
        data: data,
        dataType: "json",
        success: function(response){

            if(response.data) {
                var valueDropDown = $('#valueOfKey');
                valueDropDown.empty();
                for( var item of response.data)
                {
                    valueDropDown.append('<option value=' + item + '>' + item + '</option>');
                }
            }
        },
        error: function (jqXHR, exception) {
                            var msg = '';
                            if (jqXHR.status === 0) {
                                msg = 'Not connect.\n Verify Network.';
                            } else if (jqXHR.status == 404) {
                                msg = 'Requested page not found. [404]';
                            } else if (jqXHR.status == 500) {
                                msg = 'Internal Server Error [500].';
                            } else if (exception === 'parsererror') {
                                msg = 'Requested JSON parse failed.';
                            } else if (exception === 'timeout') {
                                msg = 'Time out error.';
                            } else if (exception === 'abort') {
                                msg = 'Ajax request aborted.';
                            } else {
                                msg = 'Uncaught Error.\n' + jqXHR.responseText;
                            }
                            $('#post').html(msg);
                                           },
                                   });
                               });
</script>
1
  • this only refers to the dropdown that was changed. You will have to either come up with a way to locate and identify the “other” dropdown, or use a separate event handler for each dropdown (easier). Commented Dec 5, 2018 at 14:00

1 Answer 1

1

You can add change event to both the dropdowns and get value of each by their id

$(document).on('change','#DropDown1, #DropDown2',function()
 {
    var value1 = $('#DropDown1').val();
    var value2 = $('#DropDown2').val();

    var data = {"key": value1,"color":value2};

     $.ajax({
        url : "value_choices",
        type: "GET",
        data: data,
        dataType: "json"
 });
Sign up to request clarification or add additional context in comments.

2 Comments

As soon as i select option from dropdown2 , the selected option doesn't show in dropdown, in fact dropdown2 immediately gets back to first option again.Though on the server i can see the correct selected option of dropdown2. @codemirror may you tell me why this is happening?
In case you want to see my actual code @codemirror, i have updated the details of my question. May be any other thing causing this issue.

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.