js 向select动态添加option,已添加,但网页不显示

yepp13 2019-04-19 10:01:17
各位大神帮忙看下,感激不尽!

HTML,js,css代码如下

HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="../static/images/3.PNG" type="image/x-icon">
<title>监控平台</title>
<link href="../static/bootstrap-3.3.7/dist/css/bootstrap.css" rel="stylesheet">
<link href="../static/bootstrap-select-1.13.9/dist/css/bootstrap-select.min.css" rel="stylesheet">
<link href="../static/css/online_offline.css" rel="stylesheet">
<link href="../static/sweetalert/sweetalert.css" rel="stylesheet">
</head>
<body class="bg">
<div class="content">
<div class="title">记录系统</div>
<hr style="color: #0c0c0c">
<div class="two_button">
<button class="btn btn-info online_button" onclick="show_online_div()">持续老化</button>
<button class="btn btn-info offline_button" onclick="show_offline_div()">新增SN</button>
</div>
{# 新增SN#}
<div class="input_item_offline">
<div>
<form class="bs-example bs-example-form" role="form">
<div class="input-group item">
<span class="input-group-addon title1">ORT/LLT NO.</span>
<input type="text" class="form-control" placeholder="">
</div>

<div class="input-group item">
<span class="input-group-addon title1">SNs</span>
<input type="text" class="form-control" placeholder="请输入SN">
</div>

<div class="input-group item">
<span class="input-group-addon title1">Testtime(H)</span>
<input type="text" class="form-control" placeholder="请输入老化时间">
</div>

</form>
</div>

<div style="margin-top: 30px;padding-top:20px;">
<a style="color: whitesmoke !important;" href="/view/">跳转到显示页面</a>
<button class="btn btn-default" style="margin-left: 100px;width: 96px">确定</button>
</div>

</div>
{# 持续老化#}
<div class="input_item">
<div class="item">
<label class="input_label">ORT/LLT NO.</label>
<select class="selectpicker main_input" id="ORT_LLT_NO" data-size="10" data-live-search="true" title="请选择ORT/LLT_NO"> </select>
</div>
<div class="item">
<label class="input_label">SNs</label>
<select class="selectpicker main_input" id="SN" data-size="10" data-live-search="true" title="请选择SN"> </select>
</div>
<div class="item">
<div style="float: left">
<label class="input_label">Testtime(H)</label>
</div>
<div style="float: left">
<input type="text" class="form-control main_input" placeholder="请输入老化时间" style="height: 35px;margin-left: 3px">
</div>
</div>

<div class="clearfix"></div>
<div style="margin-top: 30px;padding-top:20px;">
<a style="color: whitesmoke !important;" href="/view/">跳转到显示页面</a>
<button class="btn btn-default" style="margin-left: 100px;width: 96px">确定</button>
</div>
</div>

</div>

<script src="../static/js/jquery-1.11.3.min.js"></script>
<script src="../static/bootstrap-3.3.7/dist/js/bootstrap.js"></script>
<script src="../static/bootstrap-select-1.13.9/dist/js/bootstrap-select.min.js"></script>
<script src="../static/js/online_offline.js"></script>
<script src="../static/sweetalert/sweetalert.min.js"></script>
</body>
</html>


javascript:
$(document).ready(function () {
add_no();
add_sn();
});

function show_online_div() {
$(".input_item").css("display","block");
$(".input_item_offline").css("display","none");
$(".online_button").css({'background-color':"blue"});
$(".offline_button").css({'background-color':"#5bc0de"});
}

function show_offline_div() {
$(".input_item").css("display","none");
$(".input_item_offline").css("display","block");
$(".online_button").css({'background-color':"#5bc0de"});
$(".offline_button").css({'background-color':"blue"});
}

function add_no() {
// 加载ORT/LLT_NO.列表
$.ajax({
type: 'GET',
url: '/get_NO/',
data: {},
success: function (data) {
if(data['status'] === 1){
var list1 = data['list'];
for (var i =0; i < list1.length; i++){
var op = document.createElement("option"); // 新建OPTION (op)
op.appendChild(document.createTextNode(list1[i])); // 设置OPTION的 TEXT
// document.getElementById('ORT_LLT_NO').appendChild(op);
console.log(op,op.text);
$('#ORT_LLT_NO').append(op);
}
}
else if (data['status'] === 0){
swal({
title: '错误!',
text: '加载ORT/LLT_NO.失败!',
type: 'error'
})
}
else{
swal({
title: '错误!',
text: '加载ORT/LLT_NO.异常!',
type: 'error'
})
}
}
})
}

function add_sn() {
//加载对应ORT/LLT_NO下的所有sn
$("#ORT_LLT_NO").change(function() {
var NO = document.getElementById('ORT_LLT_NO').value;
// var k = document.getElementById('SN');
$('#SN').empty();
$.ajax({
type: 'GET',
url: '/get_sn/',
data: {'NO': NO},
success: function (data) {
if (data['status'] === 1) {
var list = data['list'];
for (var j = 0; j < list.length; j++) {
var op1 = document.createElement("option"); // 新建OPTION (op)
op1.appendChild(document.createTextNode(list[j])); // 设置OPTION的 TEXT
op1.value = list[j];
// console.log(op1,op1.text);
$('#SN').append(op1);
var a = document.getElementById('SN').getElementsByTagName('option');
for(var i = 0; i<a.length; i++){
console.log(i,a[i].value);
}
}
// console.log('ok');
}
else if (data['status'] === 0) {
swal({
title: '错误!',
text: '加载ORT/LLT_NO.失败!',
type: 'error'
})
}
else {
swal({
title: '错误!',
text: '加载ORT/LLT_NO.异常!',
type: 'error'
})
}
},
error: function () { alert('ooooooooo');}
})
});
}


CSS:
html{
width: 100%;
height: 100%;
}
body{
width: 100%;
height: 100%;
}
.bg{
width: 100%;
height: 100%;
background: url("../images/11.jpg") 0 0 no-repeat scroll transparent;
background-size:cover;
background-position: center;
/*border: 1px solid red;*/
}
.title{
width: 100%;
text-align: center;
color: blue;
font-size: 25px;
margin-top: 20px;
}
.title1{
text-align: center;
color: blue;
width: 120px;
font-weight: bold;
}
.main{
/*border: 1px solid red;*/
}
.main_button{
width: 100%;
font-size: 1.3vw;
}
.padding0{
/*padding: 0;*/
}
.main_item{
margin-top: 2.5vh;
font-size: 1vw;
border: 1px solid blue;
}
.main_input{
/*width: 100%;*/
width: 180px;
height: 40px;
border-radius: 5px;
}
.main_label{
height: 100%;
}
.content{
width: 400px;
height: 500px;
margin-left: -200px;
margin-top: -250px;
left: 50%;
top: 50%;
position: absolute;
background-color: #3c5277;
border-radius: 20px;
/*border: 1px solid red;*/
}
.online_button{
width: 150px;
height: 30px;
margin-left: 50px;
background-color: blue;
}
.offline_button{
width: 150px;
height: 30px;
/*margin-left: 100px;*/
}
.temperature_button{
width: 100px;
height: 30px;
}
.input_item{
width: 305px;
height: 350px;
margin-left: 50px;
/*border: 1px solid green;*/
}
.item{
/*border: 1px solid blue;*/
margin-top: 50px;

}
.input_item_offline{
width: 305px;
height: 350px;
margin-left: 50px;
/*border: 1px solid green;*/
display: none;
}
.input_item_temperature{
width: 305px;
height: 350px;
margin-left: 50px;
/*border: 1px solid green;*/
display: none;
}

.span1{
/*height: 42px;*/
padding-top: 0;
padding-bottom: 0;
}

.input_label{
width: 120px;
text-align: center;
font-size: 15px;
line-height: 33px;
border-radius: 5px;
color: white;
/*background-color: whitesmoke;*/
}
.alert_div{
width: 150px;
height: 40px;
float: left;
text-align: center;
line-height: 40px;
border-radius: 5px;
}
...全文
973 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yepp13 2019-04-19
  • 打赏
  • 举报
回复
引用 1 楼 丰云的回复:
var op = document.createElement("option"); // 新建OPTION (op) op.appendChild(document.createTextNode(list1[i])); // 设置OPTION的 TEXT // document.getElementById('ORT_LLT_NO').appendChild(op); console.log(op,op.text); $('#ORT_LLT_NO').append(op); ==>> $('#ORT_LLT_NO').append('<option>list1[i]</option>'); 不要那么哆嗦。。。用jquery,就好好用jquery, 怎么简洁怎么来
$('#ORT_LLT_NO').append('<option>' +list1[i]+'</option>') 要这么写 大佬说的对,确实要怎么简洁怎么写,不过因为是新手,有些可能不会😊 但我改了还是不显示呢。。。
丰云 2019-04-19
  • 打赏
  • 举报
回复
var op = document.createElement("option"); // 新建OPTION (op) op.appendChild(document.createTextNode(list1[i])); // 设置OPTION的 TEXT // document.getElementById('ORT_LLT_NO').appendChild(op); console.log(op,op.text); $('#ORT_LLT_NO').append(op); ==>> $('#ORT_LLT_NO').append('<option>list1[i]</option>'); 不要那么哆嗦。。。用jquery,就好好用jquery, 怎么简洁怎么来
yepp13 2019-04-19
  • 打赏
  • 举报
回复
找到问题了,是bootstrap-select这个插件加载option后要刷新
yepp13 2019-04-19
  • 打赏
  • 举报
回复
没有断点调试,但我有打印,抓取select的#SN的option打印出来都有了,但就是不显示,上面一个select有显示的
丰云 2019-04-19
  • 打赏
  • 举报
回复
不是因为样式的原因,下来选项被遮档了?
丰云 2019-04-19
  • 打赏
  • 举报
回复
断点调试了吗? 是否准确执行了? 页面下拉框是否显示出来了?

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧