Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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
Javascript JSON检索中选择框的额外未定义选项_Javascript_Php_Jquery_Json_Ajax - Fatal编程技术网

Javascript JSON检索中选择框的额外未定义选项

Javascript JSON检索中选择框的额外未定义选项,javascript,php,jquery,json,ajax,Javascript,Php,Jquery,Json,Ajax,我有一个下拉列表,其中必须填充ajax响应。我的下拉列表中有额外的未定义选项 频率下拉列表: <div class="form-group"> <label class="control-label col-xs-3" for="frequenies">Frequencies:</label> <div class="col-xs-9"> <select name="frequencie

我有一个下拉列表,其中必须填充ajax响应。我的下拉列表中有额外的未定义选项

频率下拉列表:

<div class="form-group">
        <label class="control-label col-xs-3" for="frequenies">Frequencies:</label>
        <div class="col-xs-9">
            <select name="frequencies" id="frequencies-list" class="form-control">
               <option value="">Select an option</option>
            </select>          
        </div>
</div>
success: function(data){
       alert(data);
       var x = JSON.parse(data);
       $.each(x, function() {
            $.each(this, function(k, v) {
              var frequencies = "<option value="+v.freq+">"+v.freq+"</option>";
              $(frequencies).appendTo('#frequencies-list');
           });
        });
}
[[{"freq":"1"},{"freq":"3"},{"freq":"6"},{"freq":"12"},{"freq":"24"},{"freq":"36"},{"freq":"48"},{"freq":"60"},{"freq":"72"},{"freq":"96"},{"freq":"120"},{"freq":"144"}],[{"ad_size_full":"Tab Page"},{"ad_size_full":"BRC's"},{"ad_size_full":"Cover tip"}],[{"ad_size_fractional":"2\/3 Page"},{"ad_size_fractional":"1\/2 Page Island"},{"ad_size_fractional":"1\/2 Page"},{"ad_size_fractional":"1\/3 Page"},{"ad_size_fractional":"1\/4 Page"},{"ad_size_fractional":"1\/6 Page"}],[{"color":"B&W"},{"color":"2 Color"},{"color":"3  Color \/ 4 color"},{"color":"5 Color"},{"color":"Matched Color"},{"color":"Matalic Color \/ PMS 800"}],[{"charge":"0.000000"},{"charge":"0.250000"},{"charge":"0.100000"},{"charge":"0.500000"},{"charge":"0.150000"},{"charge":"0.150000"},{"charge":"0.100000"}]]
v、 freq在我的下拉列表中生成额外的“未定义”选项。我能得到帮助吗

1
3
6
12
24
36
48
60
72
96
120
144
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined

在下一个子
数组中
,没有索引
freq
。试试-

    $.each(x, function() {
        $.each(this, function(k, v) {
          if (typeof v.freq != 'undefined') {
              var frequencies = "<option value="+v.freq+">"+v.freq+"</option>";
              $(frequencies).appendTo('#frequencies-list');
          }
       });
    });
$。每个(x,函数(){
$。每个(此,函数(k,v){
if(v.freq的类型!=“未定义”){
变量频率=“v.freq+”;
$(频率)。附加到(“#频率列表”);
}
});
});
或者只循环第一个子数组。

或者简单地:

   $.each(x, function() {
            $.each(this, function(k, v) {
              if (v.freq) // will check for undefined, null, isNaN
               {
                  var frequencies = "<option value="+v.freq+">"+v.freq+"    </option>";
                  $(frequencies).appendTo('#frequencies-list');
              }
           });
        });
$。每个(x,函数(){
$。每个(此,函数(k,v){
if(v.freq)//将检查未定义、null、isNaN
{
变量频率=“v.freq+”;
$(频率)。附加到(“#频率列表”);
}
});
});

您需要的只是频率,对吗?这样怎么样:

$.each(x, function() {
    $.each(this, function(k, v) {
      if ("freq" in v) {
          var frequencies = "<option value="+v.freq+">"+v.freq+"</option>";
          $(frequencies).appendTo('#frequencies-list');
      }
   });
});
$。每个(x,函数(){
$。每个(此,函数(k,v){
如果(v中的“频率”){
变量频率=“v.freq+”;
$(频率)。附加到(“#频率列表”);
}
});
});

尝试使用对象的第一个元素循环内部循环

$.each(x, function() {
                $.each(x[0], function(k, v) {
                      var frequencies = "<option value="+v.freq+">"+v.freq+"</option>";
                      $(frequencies).appendTo('#frequencies-list');
               });
            });
$。每个(x,函数(){
$。每个(x[0],函数(k,v){
变量频率=“v.freq+”;
$(频率)。附加到(“#频率列表”);
});
});