gpt4 book ai didi

javascript - 使用Jquery的下拉菜单的默认选择值

转载 作者:行者123 更新时间:2023-11-30 22:45:19 26 4
gpt4 key购买 nike

我有 3 个嵌套的下拉菜单,它们是从 mysql 数据库动态填充的,我使用 Jquery。我想根据我的数据库中的条目设置一个选定的值。例如:

我有一个包含许多设备的表。每个设备都有其存储位置(房间、架子、盒子)。注意:不同的房间包含不同的架子,不同的架子包含不同的盒子,这就是我使用嵌套下拉菜单的原因。每个设备旁边都有一个“更改”按钮。

我想要的是我的下拉菜单中已经选择了设备的当前存储位置。

这是我的 JS 函数的一部分(从另一个网站得到的,我对 JS 很陌生):

function populateComp(xmlindata) {

var mySelect = $('#manufacturer');
$('#printertype').disabled = false;
$(xmlindata).find("Company").each(function()
{
optionValue=$(this).find("id").text();
optionText =$(this).find("name").text();
mySelect.append($('<option></option>').val(optionValue).html(optionText));
});
}

这是我的 html 代码:

Raum:</br></br><select name="manufacturer" id="manufacturer" >
<option value="">Auswahl:</option></select>&nbsp;</br></br>
Regal:</br></br> <select name="printertype" id="printertype" disabled="disabled" >
<option value="">Auswahl:</option></select>&nbsp;</br></br>
Kiste:</br></br><select name="printermodel" id="printermodel" disabled="disabled" >
<option value="">Auswahl:</option></select></br></br>
<div id="loading" style="display: none;"></div>
<div id="output" ></div>

我的部分ajax代码:

jQuery().ready(function($){

$('#loading')
.hide() // hide it initially
.ajaxStart(function() {
$(this).show();
})
.ajaxStop(function() {
$(this).hide();
})
;
// Ajax Called When Page is Load/Ready (Load Manufacturer)
jQuery.ajax({
url: 'man_list.php',
global: false,
type: "POST",
dataType: "xml",
async: false,
success: populateComp
});

抱歉代码太多,我希望任何人都能给我一个好的解决方案,我(JS noob)可以理解:)

最佳答案

您可以在填充下拉选项时检查服务器的响应

function populateComp(xmlindata) {

var mySelect = $('#manufacturer');
$('#printertype').disabled = false;
$(xmlindata).find("Company").each(function()
{
optionValue=$(this).find("id").text();
optionText =$(this).find("name").text();
var $opt = $('<option> </option>').val(optionValue).html(optionText);
if(/* compare current data with your device here */) $opt.attr("selected", "selected");
mySelect.append($opt);
});
}

关于javascript - 使用Jquery的下拉菜单的默认选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29817706/

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