gpt4 book ai didi

javascript - 无法附加带有静态选项的下拉列表

转载 作者:行者123 更新时间:2023-12-03 12:37:59 25 4
gpt4 key购买 nike

我的 Razor View 引擎中有以下脚本:-

else if ($("#ChoiceTag").prop("checked")) {
$.getJSON("@Url.Content("~/Switch/LoadCSTag")",
function (CSData) {
var select = $("#GeneralCSID");
select.empty();
select.append($("<option/>", {
value: null
}));
$.each(CSData, function (index, itemData) {

select.append($('<option/>', {
value: itemData.Value,
text: itemData.Text
}));
select.val('@Model.Switch.ConsoleServerID');
});
});
}

我期望一旦 Jquery 函数运行,就会附加 "<option/>"下拉列表中的选项,但目前下拉列表的开头将显示一条空记录,而不是 "<options>"文本。任何人都可以建议可能是什么问题吗?谢谢

最佳答案

select.append($("<option/>", {
value: null
}));

这是您在选择列表中添加的虚拟选项。

但您从未指定该项目上应显示什么文本。您可以按照与设置以下项目的文本属性完全相同的方式对其进行设置(根据您的评论,这些项目已正确加载)

select.append($("<option/>", {
value: "", //I prefer an empty string, not sure if null is valid
text: "Select an option..."
}));

更新

您的报价:

displayed at the beginning of the drop down list, instead of the "<options>" text

我不知道你说的"<options>" text是什么意思。您的字面意思是输入字符串 <options>作为该静态选项的文本?我用过"Select an option..."作为示例,但您可以选择任何您想要的字符串。

关于javascript - 无法附加带有静态选项的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23651622/

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