gpt4 book ai didi

javascript - 使用对象数据动态创建选择菜单并实现的问题

转载 作者:行者123 更新时间:2023-12-01 01:11:16 25 4
gpt4 key购买 nike

我试图将选择菜单放入模态中,但遇到一个奇怪的问题,因为选择菜单加载了显示:无,因此它被隐藏,这是物化的正常行为吗?

选择上方的名称字段仅显示选择中的第一个条目,除非我删除检查器中的显示:无,然后允许我从选择中选择任何值,并重复上面的值。我做错了什么?

enter image description here

这是我将选择添加到的 div

<div class="input-field col s12">
<select class="custName">
</select>
<label>Select Customer</label>
</div>

我在 html 页面的末尾调用它

<script>$(document).ready(function(){
$('select').formSelect();
});</script>

在此处创建我的选择选项

const custData = (data) => {
if (data.length) {
let html = '';
data.forEach(doc => {
const cust = doc.data();
//console.log(cust);
const select = `
<select>
<option value=${cust.customerId}>${cust.name}, ${cust.address.postCode}</option>
</select>
`;
html += select;
});
customerList.innerHTML = html;
} else {
customerList.innerHTML = '<ul class="center-align">no data</ul>'
}
};

请让我摆脱痛苦

最佳答案

问题是,您的 $('select').formSelect(); 在您填充选项标记之前就被执行了。

解决方案:在您的选项标记填充代码之后进行初始化调用。

注意:不要在 custData() 函数中将每个选项标签都用 select 标签括起来

例如,这个在填充之前执行初始化语句:

<!DOCTYPE html>
<html>

<head>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
</head>

<body>
<div class="input-field col s12">
<select id="s_1" class="custName">
</select>
<label>Select Customer</label>
</div>
<script type="text/javascript">
var select = document.getElementById("s_1");
var options = "<select><option value = 1>1</option></select><select><option value = 2>2</option></select><select><option value = 3>3</option></select>";
setTimeout(function(){ select.innerHTML = options; }, 1000);


$(document).ready(function() {
$('select').formSelect();
});
</script>
</body>

</html>

这个在填充后执行初始化:

<!DOCTYPE html>
<html>

<head>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
</head>

<body>
<div class="input-field col s12">
<select id="s_1" class="custName">
</select>
<label>Select Customer</label>
</div>
<script type="text/javascript">
var select = document.getElementById("s_1");
var options = "<select><option value = 1>1</option></select><select><option value = 2>2</option></select><select><option value = 3>3</option></select>";
setTimeout(function(){ select.innerHTML = options; $('select').formSelect();}, 3000);


$(document).ready(function() {
$('select').formSelect();
});
</script>
</body>

</html>

关于javascript - 使用对象数据动态创建选择菜单并实现的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55133156/

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