Javascript MVC razor中的DropBox
我有一个DropBox,它有4个选项。这些选项是从我制作的列表中选取的。 这是列表和下拉框Javascript MVC razor中的DropBox,javascript,c#,html,asp.net-mvc,razor,Javascript,C#,Html,Asp.net Mvc,Razor,我有一个DropBox,它有4个选项。这些选项是从我制作的列表中选取的。 这是列表和下拉框 <div> @{ List<SelectListItem> listItems= new List<SelectListItem>(); listItems.Add(new SelectListItem { Text = "SearchInputPosition", Value = "
<div>
@{
List<SelectListItem> listItems= new List<SelectListItem>();
listItems.Add(new SelectListItem
{
Text = "SearchInputPosition",
Value = "1"
});
listItems.Add(new SelectListItem
{
Text = "MissingNumber",
Value = "2"
});
listItems.Add(new SelectListItem
{
Text = "ClimbStaircase",
Value = "3"
});
}
</div>
@Html.DropDownListFor(model => model.textProblem, listItems, "-- Select Status --", new { id = "c", @onchange ="changeText(c)"})
如果我从dropbox中选择一个值,我想用javascript函数显示一些文本,但它显示未定义的文本
<script>
var data = {
"1": "Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.",
"2": "You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?",
"3": "Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.",
}
function changeText(id) {
document.getElementById("content").innerHTML = data[id];
}
</script>
<div id="content"></div>
如何解决此问题?您可以为select元素绑定更改事件处理程序并使用其值
<script>
var data = {
"1": "Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.",
"2": "You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?",
"3": "Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.",
}
var select=document.getElementById('textProblem');
select.onchange=function(){
document.getElementById("content").innerHTML = data[this.value];
}
</script>
它一点作用也没有。至少不显示未定义:@GeorgeGreat,删除旧的更改事件从@Html.DropDownListFormodel=>model.textProblem,listItems中删除,-Select Status-,new{id=c,@onchange=changeTextc}可能您没有id=textProblem的元素。我知道答案确实是我所需要的。”无法将属性'onchange'设置为null'这是我遇到的错误。