Closed. This question needs details or clarity。它当前不接受答案。
                        
                    
                
            
        
            
        
                
                    
                
            
                
                    想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                
                    4年前关闭。
            
        

    

我正在尝试将变量username发送到选择器中(在选择器中显示用户名),并将变量userid作为值发送到选择器中。
你能帮助我吗 ?这是我的代码:

<select id="mySecond" name="continent">
    <option value="">-----------------</option>
    <option  value="">-----------------</option>
    </select>


<script>

function displayuser() {
    var x = document.getElementById("mySelect").value;
    document.getElementById("demo").innerHTML = "Mon id: " + x;

  $.ajax({
      url: "https://cubber.zendesk.com/api/v2/organizations/"+x+"/users.json",
      type: 'GET',
      dataType: 'json',
            contentType:'application/json',
            secure: true,
            beforeSend: function (xhr) {
                xhr.setRequestHeader ("Authorization", "Basic " + btoa("[email protected]"));
            },
            success: function (data){
                for (i = 0; i < data.users.length; i++) {
                console.log(data.users[i].name);
                var username = data.users[i].name;
                console.log(data.users[i].id);
                var userid = data.users[i].id;
                }
            },
  });
}

</script>

最佳答案

尝试这个:

success: function (data)  {
  $.each(data, function(){
        $("#mySecond").append('<option value="'+ this.userid +'">'+ this.username +'</option>')
   })
}

09-25 19:33