我有一个具有4个选项的DropBox。这些选项来自我制作的列表。
这是列表和保管箱

 <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)"})


如果我从保管箱中选择一个值,则要显示一些带有javascript函数的文本,但该文本显示为undefined。

<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>


我该怎么做才能解决此问题?

最佳答案

您可以为change元素绑定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>

关于javascript - MVC Razor 中的DropBox,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43775095/

10-12 13:40