简体   繁体   English

在 jquery 中使用两个下拉菜单?

[英]Using two dropdowns in jquery?

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.现在我有两个下拉列表,而不是第二个下拉列表的 id 是 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>

You can add change event to both the dropdowns and get value of each by their id您可以将更改事件添加到两个下拉列表中,并通过它们的 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"
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM