weixin_33722405 2018-12-05 13:48 采纳率: 0%
浏览 21

在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. 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.
 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.
' + jqXHR.responseText;
                            }
                            $('#post').html(msg);
                                           },
                                   });
                               });
</script>
  • 写回答

1条回答 默认 最新

  • ?Briella 2018-12-05 13:57
    关注

    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"
     });
    
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码