【发布时间】:2016-10-30 03:53:25
【问题描述】:
我终其一生都无法弄清楚如何使用 ASP.Net 更新引导控件。
我有以下代码:
@{
Layout = null;
}
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title Mark Hub</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
<script>
var url = "http://localhost:51520/api/teacher/"
function getTerms(course) {}
//get a reference to the select element
$select = $('#termSelect');
//request the JSON data and parse into the select element
$.ajax({
url: url + "global/currentterms/" + course ,
dataType:'JSON',
success:function(data){
//clear the current content of the select
$select.html('');
//iterate over the data and append a select option
$.each(data.person, function(key, val){
$select.append('<option id="' + val.id + '">' + val.name + '</option>');
})
},
error:function(){
//if there is an error append a 'none available' option
$select.html('<option id="-1">none available</option>');
}
});
</script>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Mark Hub", "Index", "Teacher", null, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Teacher", "Index", "Teacher")</li>
<li>@Html.ActionLink("Admin", "Index", "Admin")</li>
</ul>
</div>
</div>
</div>
<div class="section">
<div class="container">
<div class="row">
<div class="col-md-1 text-left">
<select class="form-control" id="courseSelect" onclick="getTerms()">
<option>DEC 10</option>
</select>
</div>
<div class="col-md-1 text-left">
<select class="form-control" id="termSelect">
</select>
</div>
</div>
</div>
</div>
<div class="section">
<div class="container">
<div class="row">
<div class="col-md-12">
<hr />
<footer>
<p>© @DateTime.Now.Year</p>
</footer>
</div>
</div>
</div>
</div>
</body>
</html>
我正在尝试从courseSelect 控件中获取用户选择的值(为了方便起见,我目前只有一个),将其传递给我的Javascript 函数getTerms,调用Web api 并填充@987654324 @控制从返回的JSON动态。
从我的courseSelect 组合框中选择一个值不会更新我的termSelect 组合框。
我将如何修复我的代码?
使用更新代码进行编辑以反映答案中的更新
<script>
var url = "http://localhost:51520/api/teacher/"
$(function(){
$('#courseSelect').on('change',function(){
// This is equal to your getTerms function
var course = $('#courseSelect').val();
//request the JSON data and parse into the select element
$.ajax({
url: url + "global/currentterms/" + course,
dataType:'JSON',
success:function(data){
//clear the current content of the select
$select.html('');
//iterate over the data and append a select option
$.each(data.termID, function(key, val){
$select.append('<option id="' + val.id + '">' + val.name + '</option>');
})
},
error:function(){
//if there is an error append a 'none available' option
$select.html('<option id="-1">none available</option>');
}
})
})});
</script>
我的代码仍然无法运行,当我在 VS 中添加断点时,不会调用该函数来从选择框 courseSelect 中选择一个值。
【问题讨论】:
-
有什么问题?
-
现在您将 Ajax 结果附加到
$('#courseSelect');元素而不是termSelect -
@Reddy 将
courseSelect固定为termSelect。 @StephenMuecke 更新了问题以更好地反映问题。 -
你还没有定义问题。你打你的控制器方法吗?什么是控制器方法,它返回什么?如果您在浏览器控制台中遇到任何错误怎么办?
-
现在我看到您没有将下拉列表中的选定值传递给您的 API 调用
标签: javascript c# asp.net asp.net-mvc twitter-bootstrap