Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery使用JSON数据填充DropDownList_Jquery_Json_Drop Down Menu - Fatal编程技术网

JQuery使用JSON数据填充DropDownList

JQuery使用JSON数据填充DropDownList,jquery,json,drop-down-menu,Jquery,Json,Drop Down Menu,假设我从后端服务层返回了以下数据: [“2”、“3”、“7”、“14”] 第一个问题。这被认为是有效的JSON数据吗?说是的,但我在谷歌上找不到它的任何参考资料,所以,等等 值的有序列表。在大多数语言中,这是以数组、向量、列表或序列的形式实现的 我希望能够使用JQuery将这些值添加到已经存在的DropDownList对象OnLoad中 $.getJSON("http://www.example.com/path-to-get-data", function(data) { //itera

假设我从后端服务层返回了以下数据:

[“2”、“3”、“7”、“14”]

第一个问题。这被认为是有效的JSON数据吗?说是的,但我在谷歌上找不到它的任何参考资料,所以,等等

值的有序列表。在大多数语言中,这是以数组、向量、列表或序列的形式实现的

我希望能够使用JQuery将这些值添加到已经存在的DropDownList对象OnLoad中

$.getJSON("http://www.example.com/path-to-get-data", function(data) { 
  //iterate through each object and add it to the list.
  //ideally, the key and value (string to display to the end user) would be the same.
});
我看了一下,但它有对象,而不仅仅是一个数组。我应该使用
parseJSON
还是
getJSON

提前谢谢

var-arr=[2,3,7,14];
var arr = [ 2, 3, 7, 14];
$.each(arr, function(index, value) {
     ('#myselect').append($('<option>').text(value).attr('value', index));
});
$。每个(arr,函数(索引,值){ (“#myselect”).append($('').text(value.attr('value',index)); });
还请看一下C先生的解决方案:

$('<option></option>').val(item).html(item)
$(“”).val(item).html(item)

他操作
选项的方式对我来说是新的,而且更加优雅。

这不是正确的JSON,它必须是这种风格:

{"0": "2", "1" : "3", "2" : "7", "3" : "14"}
您可以使用该示例来处理您的响应:

var response = "[2, 3, 7, 14]";
eval("var tmp = " + response); 
console.log(tmp); 

我的解决方案是基于我的想法

//填充下拉列表
$.getJSON(“http://www.example.com/api/service,函数(数据){
$。每个(数据、功能(索引、项目){
$('#dropDownList')。追加(
$('').val(项目).html(项目)
);
});
});

这是HiddenField声明,用于存储JSON字符串

   <asp:HiddenField ID="hdnBankStatus" runat="server" />
这些行将逐个使用JSON字符串的元素,并用值填充下拉列表

   $(document).ready(function () {  

   var myOptions = $.parseJSON($('#hdnBankStatus').val());
        var mySelect = $('#optStatus');
        $.each(myOptions, function (val, text) {
            mySelect.append(
    $('<option></option>').val(text).html(val)
        );
        });
   }
$(文档).ready(函数(){
var myOptions=$.parseJSON($('hdnBankStatus').val();
var mySelect=$('optStatus');
$.each(myOptions,function(val,text){
mySelect.append(
$('').val(文本).html(val)
);
});
}

对我来说,这一个很有效

$("#ddlCaseType").append("<option>" + txt + "</option>");
$(“#ddlCaseType”).append(“+txt+”);
var-arr=[2,3,7,14];
var下拉列表=(“#myselect”);
dropdown.empty();
追加($('--请选择--');
$。每个(arr,函数(索引,值){
追加($(''+值+'');
});`

我认为这是无效的,有效的JSON将类似于{“key:“2”,“key:“3”,“key:“7”,“key:“14”}
$。parseJSON(“[“2”,“3”,“7”,“14”])”)
将返回一个数组。最好使用getJSON并迭代数组中的每个项,并将其添加到对象$(“#ddlCaseType”)。追加(“+txt+”);它抛出对象不支持.append“您的jQuery版本是什么?您的浏览器是什么?
$。append
从很早的版本就开始提供。您的DOM选择器(即
$('#myselect')
部分)对吗?我在回答的第3行发现了一个小的打字错误。
('#myselect')append($).text前面的
$
(value).attr('value',index));
缺失。这非常有用,谢谢。这应该可以做到
   sl = new SortedList();
   hdnBankStatus.Value = jsonSerialiser.Serialize(sl);
   $(document).ready(function () {  

   var myOptions = $.parseJSON($('#hdnBankStatus').val());
        var mySelect = $('#optStatus');
        $.each(myOptions, function (val, text) {
            mySelect.append(
    $('<option></option>').val(text).html(val)
        );
        });
   }
$("#ddlCaseType").append("<option>" + txt + "</option>");
 var arr = [ 2, 3, 7, 14];
 var dropdown = ('#myselect');
 dropdown.empty();
 dropdown.append($('<option value=''>--Please Select--</option>'));
 $.each(arr, function(index, value) {
 dropdown.append($('<option value='+index+'>'+value+'</option>'));
 });`