gpt4 book ai didi

javascript - 从标签比较中获取 json id

转载 作者:行者123 更新时间:2023-11-30 10:18:35 25 4
gpt4 key购买 nike

我正在尝试比较一个变量(部门)以查看它是否与我的 JSON 数组中的“标签”相匹配。如果是这样,我希望它成为我选择列表中的默认选择,但我不知道如何获取匹配标签的“id”

var department = '<?php echo $dept; ?>';
//var department = 'Deployment'; <--- thats what $dept is

////// PROJECT CHANGE
$('#projectSelect').on('change', function() {

var selectedProject = $('#projectSelect').val();
$.getJSON('json.php?task='+selectedProject, function(data) {
var select = $('#taskSelect');
select.empty();

$.each(data, function(key, val){
select.append("<option value='"+this.id+"'>"+this.label+"</option>");
});

/******** THIS IS WHERE I NEED HELP ... **********/
if (department == this.label) {
$("#taskSelect").val([this.id]).select2();
}
/*************************************************/

var taskSelected = $('#taskSelect');
$("#taskSelect").select2().select2('value', taskSelected);

});

})

最佳答案

你的意思是这样的吗?

$.each(data, function(key, val){
var opt = $("<option value='"+this.id+"'>"+this.label+"</option>");

if(department == this.label)
opt.attr("selected","selected");

select.append(opt);
});

关于javascript - 从标签比较中获取 json id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22539306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com