【问题标题】:how to get JSON value through Dropdown如何通过 Dropdown 获取 JSON 值
【发布时间】:2015-02-16 11:00:56
【问题描述】:

我们有这样的 JSON

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

我们需要在下拉列表中显示用户名,如果我在下拉列表中选择名称,我们需要获取用户 ID。如果我们选择 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>');

        });
}

我们需要像这样发布 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();

    }
    });

请指导我。

【问题讨论】:

  • 有写选择框改变事件代码吗?
  • 你的下拉菜单在哪里
  • @Sandeeproop 按钮操作

标签: javascript jquery json cordova drop-down-menu


【解决方案1】:

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

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

那么当#date发生变化时,你就可以抓取值($('#date').val()),应该是userid。

【讨论】:

    【解决方案2】:

    在 option.get selected option on change event of select box(#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();
    
      }
      });
     })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多