Javascript 如何从select中的json获取对象数据?
因此,我有一个select元素,并附加从API返回的数据Javascript 如何从select中的json获取对象数据?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,因此,我有一个select元素,并附加从API返回的数据 function getHeadData() { $("#itemSelect").empty(); if (headitemData.length < 1) { $.getJSON("http://localhost:9000/api/helmets", function (key, value) { console.log("request helmets");
function getHeadData() {
$("#itemSelect").empty();
if (headitemData.length < 1) {
$.getJSON("http://localhost:9000/api/helmets", function (key, value) {
console.log("request helmets");
var item = "";
headitemData = key;
var len = key.length;
for (let i = 0; i < len; i++) {
item += '<option value="' + key + '">' + key[i].Name + '</option>';
}
$('#itemSelect').append(item);
});
}
else {
clearIndex(headitemData);
}
}
由于您希望在代码的其他区域中使用数据,请使用闭包来创建一个环境,使您的变量不会泄漏到全局空间中。例如,使用回调函数:
(function () {
var myData;
function test(callback) {
$.getJSON("http://localhost:9000/api/helmets",function (data) {
callback(data);
});
}
test(function (data) {
myData = data;
});
function getHeadData() {
$("#itemSelect").empty();
if (headitemData.length < 1){
console.log("request helmets");
var item = "";
headitemData = myData;
var len = myData.length;
for (let i = 0; i < len; i++) {
item += '<option value="' +myData+ '">' + myData[i].Name + '</option>';
}
$('#itemSelect').append(item);
}
else {
clearIndex(headitemData);
}
}
$("#itemSelect").on("change",function(){
var value = $(this).val();
var newData = myData.filter(item=>{
if(myData.Name==value){
return item;
}
});
console.log(newData.Icon);
});
myData被缓存为特定于该闭包的全局变量。注意,只有在回调完成后,其他函数才能使用该变量。由于您希望在代码的其他区域使用数据,请使用闭包来创建一个环境,使您的变量不会泄漏到全局空间。例如,使用回调函数:
(function () {
var myData;
function test(callback) {
$.getJSON("http://localhost:9000/api/helmets",function (data) {
callback(data);
});
}
test(function (data) {
myData = data;
});
function getHeadData() {
$("#itemSelect").empty();
if (headitemData.length < 1){
console.log("request helmets");
var item = "";
headitemData = myData;
var len = myData.length;
for (let i = 0; i < len; i++) {
item += '<option value="' +myData+ '">' + myData[i].Name + '</option>';
}
$('#itemSelect').append(item);
}
else {
clearIndex(headitemData);
}
}
$("#itemSelect").on("change",function(){
var value = $(this).val();
var newData = myData.filter(item=>{
if(myData.Name==value){
return item;
}
});
console.log(newData.Icon);
});
myData被缓存为特定于该闭包的全局变量。注意:只有在回调完成后,其他函数才能使用该变量。您可以为选项设置数据,如:
您可以为您的选项设置数据,如:
修改版本如下:
function getHeadData() {
$("#itemSelect").empty();
if (headitemData.length < 1) {
$.getJSON("http://localhost:9000/api/helmets", function (key, value) {
console.log("request helmets");
var item = "";
headitemData = key;
var len = key.length;
for (let i = 0; i < len; i++) {
var icon=key[i].Icon;
item += '<option data-icon="'+icon+'" value="' + key + '">' + key[i].Name + '</option>';
}
$('#itemSelect').append(item);
});
}
else {
clearIndex(headitemData);
}
}
更改选择时回显的步骤
$("#itemselect").change(function(e) {
var optionSelected = $("option:selected", this);
console.log (optionSelected.data('icon'));
});
修改版本如下:
function getHeadData() {
$("#itemSelect").empty();
if (headitemData.length < 1) {
$.getJSON("http://localhost:9000/api/helmets", function (key, value) {
console.log("request helmets");
var item = "";
headitemData = key;
var len = key.length;
for (let i = 0; i < len; i++) {
var icon=key[i].Icon;
item += '<option data-icon="'+icon+'" value="' + key + '">' + key[i].Name + '</option>';
}
$('#itemSelect').append(item);
});
}
else {
clearIndex(headitemData);
}
}
更改选择时回显的步骤
$("#itemselect").change(function(e) {
var optionSelected = $("option:selected", this);
console.log (optionSelected.data('icon'));
});
这是jsondata还是xmldata?这是一个JSON示例吗?你能用一个可运行的示例更新你的文章吗?这似乎不完整。这是jsondata还是xmldata?这是一个JSON示例吗?你能用一个可运行的示例更新你的帖子吗?这似乎不完整。我希望能够从我创建的JSON数据中获取图标值provided@MarkDenom如果您将Icon属性作为属性传递给,那么您将在更改事件中获得该属性。我希望能够从我创建的JSON数据中获取图标值provided@MarkDenom如果将Icon属性作为属性传递给,则在更改时将获得此属性事件
$("#itemselect").change(function(e) {
var optionSelected = $("option:selected", this);
console.log (optionSelected.data('icon'));
});