我有一个下拉列表,当我从下拉列表中选择项时,我的列表框将填充一组来自json的项,列表框中的某些项没有reak number值,我只想要那些具有实数值的项..下面是我的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" } ]
最佳答案
所以需要对添加到控件中的字符串进行验证?
试试这个:
Validate decimal numbers in JavaScript - IsNumeric()
关于javascript - 我的列表框中仅允许包含实数的那些项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36890717/