如何通过 Dropdown 获取 JSON 值

how to get JSON value through Dropdown

我们有JSON这样

"[{\"UserID\":1,\"Name\":\"demo\"},{\"UserID\":4,\"Name\":\"ekova\"},{\"UserID\":2,\"Name\":\"Himansu-it\"},{\"UserID\":3,\"Name\":\"Himansu-it Services\"}]"

我们需要在下拉列表中显示用户名,如果我 Select 下拉列表中的名称 我们需要 UserID.Like 如果我们 Select ekvoa 我们需要获得用户 ID 4

我们这样显示用户名下拉

function selectitems(){

     var getCustomerIDs = jQuery.parseJSON( customerID );

        $(getCustomerIDs).each(function() {
    console.log(this.UserID);
    console.log(this.Name);
    $('#date').append('<option>'+this.Name+'</option>');

        });
}

我们需要post这样的UserID

$.ajax({

            url:'http://www.himansuit.com/DemoSalesApp/MyService.svc/GetCurrentGeoLocationsByUserID/'+userID,
            dataType: 'jsonp',
            type:'get',
            cache:false,
            timeout: 10000,
            error: function(x, t, m) {
        if(t==="timeout") {
            debugger;
            $('#alertmessage').empty();

             alert("NO Internet Connection");
          // withOutNetConnection();
        } else {
            //alert(t);
        }},
            success:function(data) {
                debugger;
                GetAllElementsjson=data;

       elements();

    }
    });

请指导我。

将选项的值设置为 UserID,如下所示:

$('#date').append('<option value="'+this.UserID+'">'+this.Name+'</option>');

然后#date改变时,可以抓取值($('#date').val()),应该是userid

在 option.get selected 选项 select 框(#date')

的更改事件中添加用户 ID 作为值
$('#date').append('<option value='+this.UserID+'>'+this.Name+'</option>');

在 (#date) 的更改事件上调用 ajax。

 $('#date').on('change',function(){
   var  userID = $(this).val();
    $.ajax({
        url:'http://www.himansuit.com/DemoSalesApp/MyService.svc/GetCurrentGeoLocationsByUserID/'+userID,
        dataType: 'jsonp',
        type:'get',
        cache:false,
        timeout: 10000,
        error: function(x, t, m) {
    if(t==="timeout") {
        debugger;
        $('#alertmessage').empty();

         alert("NO Internet Connection");
      // withOutNetConnection();
    } else {
        //alert(t);
    }},
        success:function(data) {
            debugger;
            GetAllElementsjson=data;

   elements();

  }
  });
 })