只允许那些在我的列表框中有实数的项目

Allowing only those items which have real numbers inside my list box

本文关键字:列表 实数 项目 我的 许那些 那些      更新时间:2023-09-26

我有一个下拉列表,当我从下拉列表中选择项目时,我的列表框中填充了来自json的项目集,并且该列表框中的某些项目没有reak数字值,我只想要那些具有实数值的项目..下面是我的JavaScript。请建议

$(document).ready(function() {
    $.ajax({
        url: "avb.json",
        dataType: "json",
        success: function(obj) {
            console.log("obj--", obj)
            var jsObject = obj;
            var usedNames = [];
            $('<option>', {
                text: 'Select your Option',
                value: '',
                selected: 'selected',
                disabled: 'disabled',
                location: 'fixed'
            }).appendTo('#dropdown1')
            $.each(obj, function(key, value) {
                if (usedNames.indexOf(value.name) == -1) {
                    $("#dropdown1").append("<option value=" + key + ">" + value.name + "</option>");
                    usedNames.push(value.name);
                }
            });
            $('#dropdown1').change(function() {
            if (!isNaN(this.value) ){
                $('#listbox').toggle(this.value != "");
                }
            });
            $('#dropdown1').change(function() {

                $('#listbox').empty();
                $('<option>', {
                    text: 'Select your List Option',
                    value: '',
                    selected: 'selected',
                    disabled: 'disabled'
                }).appendTo('#listbox');
                var selection = $('#dropdown1 :selected').text();
                console.log("as".selection);
                $.each(jsObject, function(index, value) {
                    console.log("%o",value)
                    if (value['name'] == selection) {
                        var optionHtml = '';
                        for (var i = 1; i <=20; i++) {
                            var attr = 'attr' + ('000' + i).substr(-3);
                            var val = 'val' + ('000' + i).substr(-3);
                            optionHtml += '<option value="' + attr + '" data-val="'+value[val]+'">' + value[attr] + '</option>';
                        }

                        $("#listbox").css("width", "500px")
                        $("#listbox").css("height", "300px")
                        $('#listbox').append(optionHtml);
                        return false;
                    }
                    var selectedOption = $(this).find('option:selected');
                    console.log(selectedOption);
                });
            });
            $("#listbox").on("click", function() {
                console.log("asd", $('#listbox option:selected').attr('data-val'));
                var zxv =  $('#listbox option:selected').attr('data-val')
                $(".bar").attr("y",zxv)
                console.log("test", $(".bar").attr("y",zxv))
            //   $(".bar").attr("height",'100')
            })
        }
    });
});
my json
[ {
 "name": "ABC",
 "date": 1459461600000, 
"attr001": "SIGN1",
 "val001": "60", 
"attr002": "SIGN2",
 "val002": "5",
 "attr003": "SIGN3", 
"val003": "100.00", 
"attr004": "SIGN4", 
"val004": "0",
"attr005": "SIGN5", 
"val005": "Traesnotfound" 
}, 
{
"name": "ABC",
 "date": 1461176704000,
 "attr001": "SIGN1",
 "val001": "200", 
"attr002": "SIGN2", 
"val002": "70", 
"attr003": "SIGN3", 
"val003": "100.00",
 "attr004": "SIGN4",
 "val004": "670",
 "attr005": "SIGN5", 
"val005": "Traceinvalid"
 }, 

 { "name": "XYZ", 
"date": 1459125900000, 
"attr001": "VISSE1",
 "val001": "100", 
"attr002": "VISSE2",
 "val002": "7",
 "attr003": "VISSE3",
 "val003": "300.00",
 "attr004": "VISSE4",
 "val004": "160",
 "attr005": "SIGN5", 
"val005": "not found"
 },
 { "name": "XYZ",
 "date": 1459461600000,
 "attr001": "VISSE1", 
"val001": "50", 
"attr002": "VISSE2",
 "val002": "70",
 "attr003": "VISSE3",
 "val003": "300.00",
 "attr004": "VISSE4",
 "val004": "230",
  "attr005": "SIGN5", 
"val005": "found"
 },
{ "name": "XYZ", 
"date": 1459461900000, 
"attr001": "VISSE1",
"val001": "300", 
"attr002": "VISSE2",
 "val002": "10", 
"attr003": "VISSE3", 
"val003": "500.00",
 "attr004": "VISSE4",
 "val004": "350",
 "attr005": "SIGN5", 
"val005": "not found" } ]

所以你需要验证添加到控件中的字符串吗?

试试这个:在 JavaScript 中验证十进制数 - IsNumeric()

你必须检查值是否是数字是 isNaN(value.name) 如果提供的值不是数字,则此函数将返回 true,否则为 false。请查看 isNaN() 详细信息此链接以获取更多详细信息。

if (!isNaN(value.name) ) {
      $("#dropdown1").append("<option value=" + key + ">" +
                value.name + "</option>");
      usedNames.push(value.name);
}

从选项调用波纹管函数中删除非十进制值

function removeNonNumberEmptyOptions(){
   for(var i=0; i< $("#dropdown1 option").length;i++){
      if (isNaN($("#dropdown1 option")[i].value)){
          $("#dropdown1 option[value=" 
                     +$("#dropdown1 option")[i].value + "]").remove()
      }
   }
}

结帐 https://fiddle.jshell.net/6b9ne2y0/6/根据您的期望链接其工作

你为for循环做硬编码,它应该是

 userPorperties = Object.getOwnPropertyNames(value);
                for (var i = 0; i <= userPorperties.length -1; i++) {
                    if (!isNaN(value[userPorperties[i]]))
                      optionHtml += '<option value="' + userPorperties[i] + '"          
                      data-val="' + value[userPorperties[i]]  + '">' 
                      + userPorperties[i] + "=>" 
                      +value[userPorperties[i]]+ '</option>';
                }