gpt4 book ai didi

javascript - 更改选择值 Javascript

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

我有一个用以下数据动态填充 JSON 数据的选择:元素、1、2、3 ...一旦选择,该选择就会隐藏,因为再次显示按钮,但我无法使用元素值,它与用户选择的值一起显示,我如何更改 JavaScript 中选择的值?HTML

 <div id="tablaSelect">
<table id=tabla2 style="width:80%" >
<tr>
<th>
<div class="styled-select">
<select class="select" id="boards">
</select>
</div>
</th>
</tr>
</table>

Javascript 选择:

$('#boards')
.append($("<option></option>")
.attr("value",'')
.text("Eelement"));

$.each(boards, function(index, value) {
$('#boards')
.append($("<option></option>")
.attr("value",value.id)
.text(value.name));
arrayBoards.push(value.id);
});

要改变的Javascript

function cambiar(){
document.getElementById("tablaSelect").style.display="block";
document.getElementById("select").value = '';
}

最佳答案

如果有一个具有该值的选项(查看您的代码,确实存在),您的 document.getElementById("select").value = ''; 就会起作用,但是 元素的 idboards,而不是 select:

$('#boards')
.append($("<option></option>")
.attr("value",'')
.text("Eelement"));

var boards = [
{id: 1, name: "One"},
{id: 2, name: "Two"},
{id: 3, name: "Three"},
{id: 4, name: "Four"}
];
var arrayBoards = [];

$.each(boards, function(index, value) {
$('#boards')
.append($("<option></option>")
.attr("value",value.id)
.text(value.name));
arrayBoards.push(value.id);
});

function cambiar(value){
document.getElementById("tablaSelect").style.display="block";
document.getElementById("boards").value = value;
}

cambiar("1");
setTimeout(function() {
cambiar("");
}, 800);
<div id="tablaSelect">
<table id=tabla2 style="width:80%" >
<tr>
<th>
<div class="styled-select">
<select class="select" id="boards">
</select>
</div>
</th>
</tr>
</table>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

或者,因为您使用的是 jQuery,所以使用它的 val : $("#boards").val("");

$('#boards')
.append($("<option></option>")
.attr("value",'')
.text("Eelement"));

var boards = [
{id: 1, name: "One"},
{id: 2, name: "Two"},
{id: 3, name: "Three"},
{id: 4, name: "Four"}
];
var arrayBoards = [];

$.each(boards, function(index, value) {
$('#boards')
.append($("<option></option>")
.attr("value",value.id)
.text(value.name));
arrayBoards.push(value.id);
});

function cambiar(value){
document.getElementById("tablaSelect").style.display="block";
$("#boards").val(value);
}

cambiar("1");
setTimeout(function() {
cambiar("");
}, 800);
<div id="tablaSelect">
<table id=tabla2 style="width:80%" >
<tr>
<th>
<div class="styled-select">
<select class="select" id="boards">
</select>
</div>
</th>
</tr>
</table>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

关于javascript - 更改选择值 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50813374/

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