从jquery更改事件中填充2个下拉列表

时间:2018-02-24 13:47:39

标签: javascript jquery

我有一个改变事件,它本身运作良好。但是,我想要做的是,在更改事件中,使用2个不同的$ .get调用php更新2个不同的div。例如,用户选择公司名称。它存储在value的全局变量中。然后我需要在mysql中查询2个不同的查询,并将结果放在2个不同的div中。

我知道这段代码永远不会起作用,所以我需要专家的指导。

这很难解释,所以希望代码能够对它有所了解。如果您需要更多信息,请告诉我。非常感谢

$(function() {
  $(document).on('change', '.rtvcompany', function() {
    value = $(this).val();
    $(this).after('<div id="loader"><img src="/domain/admin/images/loader.gif" alt="loading files" /></div>');
    $.get('/domain/admin/requests/boxes/retrieve/loadboxRtvcompany.php?rtvcompany=' + value, function(data) {
      console.log(value);
      $(".rtvdept").html(data);
      $('#loader').slideUp(200, function() {
        $(this).remove();
        $(".rtvdept").val('');
        $(".rtvdept").attr("data-placeholder", "Select your department");
        $(".rtvdept").trigger("chosen:updated");
      });
    });
  });
});

$(function() {
      $(document).on('change', '.rtvdept', function() {
        value = $(this).val();
        $(this).after('<div id="loader"><img src="/domain/admin/images/loader.gif" alt="loading files" /></div>');
        $.get('/domain/admin/requests/boxes/retrieve/loadboxRtvaddr.php?rtvaddr=' + value, function(data) {
          $("#address").html(data);
          $('#loader').slideUp(200, function() {
            $(this).remove();
            //console.log(data);
            $("#address").val('');
            $("#address").attr("data-placeholder", "Select your address");
            $("#address").trigger("chosen:updated");
          });
        });
      });
    });

    $(function() {
      $(document).on('change', '.rtvdept', function() {
        //value = $(this).val();
        $(this).after('<div id="loader"><img src="/domain/admin/images/loader.gif" alt="loading files" /></div>');
        $.get('/domain/admin/requests/boxes/retrieve/loadboxRtvbox.php?rtvbox=' + value, function(data) {
          //console.log(value);
          $("#box_rtv").html(data);
          $('#loader').slideUp(200, function() {
            $(this).remove();
            //console.log(data);
            $("#box_rtv").val('');
            //$("#box_rtv").attr("data-placeholder", "Select your new box");
            $("#box_rtv").trigger("chosen:updated");
          });
        });
      });
    });

1 个答案:

答案 0 :(得分:1)

试试:

 $(function() {
$(document).on('change', '.rtvcompany', function() {
value = $(this).val();
$(this).after('<div id="loader"><img src="/domain/admin/images/loader.gif" alt="loading files" /></div>');
$.get('/domain/admin/requests/boxes/retrieve/loadboxRtvcompany.php?rtvcompany=' + value, function(data) {
  console.log(value);
  $(".rtvdept").html(data);
  $('#loader').slideUp(200, function() {
    $(this).remove();
    $(".rtvdept").val('');
    $(".rtvdept").attr("data-placeholder", "Select your department");
    $(".rtvdept").trigger("chosen:updated");
  });
});
    value = $(this).val();
    $(this).after('<div id="loader"><img src="/domain/admin/images/loader.gif" alt="loading files" /></div>');
    $.get('/domain/admin/requests/boxes/retrieve/loadboxRtvaddr.php?rtvaddr=' + value, function(data) {
      $("#address").html(data);
      $('#loader').slideUp(200, function() {
        $(this).remove();
        //console.log(data);
        $("#address").val('');
        $("#address").attr("data-placeholder", "Select your address");
        $("#address").trigger("chosen:updated");
      });
    });
    value = $(this).val();
    $(this).after('<div id="loader"><img src="/domain/admin/images/loader.gif" alt="loading files" /></div>');
    $.get('/domain/admin/requests/boxes/retrieve/loadboxRtvbox.php?rtvbox=' + value, function(data) {
      //console.log(value);
      $("#box_rtv").html(data);
      $('#loader').slideUp(200, function() {
        $(this).remove();
        //console.log(data);
        $("#box_rtv").val('');
        //$("#box_rtv").attr("data-placeholder", "Select your new box");
        $("#box_rtv").trigger("chosen:updated");
      });
    });
  });
});