我有一个用以下数据动态填充JSON数据的选择:元素,1,2,3 ...一旦被选中,选择被隐藏,因为再次显示一个按钮,但我无法显示它元素值,用用户选择的值显示,如何从Javascript中更改select的值? HTML
using (var context = new VASTEntities())
{
var query = context.APLCN_TRCKR;
foreach(var filter in filters)
{
query = query.Where(filter);
}
var result = query.ToList();
if (result.Count() == 0)
{
throw new Exception("No Applications Found.");
}
foreach (var a in result)
{
searchAppsObj.Add(Translator.TranslateReqObjToBO.TranslateDTOToSearchApp(a));
}
context.Connection.Close();
return searchAppsObj;
}
Javascript选择:
<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);
});
答案 0 :(得分:1)
如果有一个带有该值的选项(并查看您的代码,那么)document.getElementById("select").value = '';
会有效,但该元素的id
为boards
,不是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);
&#13;
<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>
&#13;
或者,既然您正在使用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);
&#13;
<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>
&#13;
答案 1 :(得分:0)
是您可以像这样动态更改下拉列表的值
function cambiar(){
document.getElementById("tablaSelect").style.display="block";
$("#boards").val(Any value here);
}