Lotus@ 2017-09-18 07:24 采纳率: 100%
浏览 21

jQuery与Ajax

I need to get the dropdown box value based on the previous dropdown box value in my code I need to get the first name and based on that I need to get last name in another drop down box please find my code.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    var List;
     jQuery.ajax({
        url: "http://some ip address/igasp-admin/api.php/um_users?&transform=1",
        type: "GET",
        dataType: "json",
        async: false,
        success: function (data) {
            var i;
        List = data.um_users
            $('#userData').empty();
        for (i in List ) {
                $('#userData').append('<option value="'+ List[i].last_name + '">' + List[i].first_name + '</option>');
                $('#userorgData').append('<option value="">'+ List[i].last_name + '</option>');
            }
        }
    });


});
</script>
</head>
<body>
<select  style="width: 250px;" id="userData">
 </select>
<select  style="width: 250px;" id="userorgData">
</select> 
</body>
</html>
  • 写回答

2条回答 默认 最新

  • local-host 2017-09-18 07:34
    关注

    You mean

    $('#userData').append('<option value="'+ List[i].last_name + '">' + List[i].first_name + '</option>');
    $('#userorgData').append('<option value="'+ List[i].last_name + '">' + List[i].last_name + '</option>');
    

    and then have

    $('#userData').on("change",function() {
       $('#userorgData').val(this.value); // both have the same lastname as value
    });
    

    As in

    $(function() {
      $('#userData').on("change", function() {
        $('#userorgData').val(this.value); // both have the same lastname as value
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <select id="userData">
      <option value="">Please select</option>
      <option value="Lennon">John</option>
      <option value="McCartney">Paul</option>
      <option value="Harrison">George</option>
      <option value="Star">Ringo</option>
    </select>
    <select id="userorgData">
      <option value="">Please select</option>
      <option value="Lennon">Lennon</option>
      <option value="McCartney">McCartney</option>
      <option value="Harrison">Harrison</option>
      <option value="Star">Star</option>
    </select>

    </div>
    
    评论

报告相同问题?