jquery - 将下拉列表中的 2 个参数传递给控制器。
问题描述
我的页面上有 2 个下拉列表,我需要的是当两个下拉列表都选择了值时,将该值传递给控制器。以及如何阻止\禁用新请求,直到前一个请求未完成?
这是我拥有的一些代码。看法:
@using (Ajax.BeginForm("IndexStb", "GroupStatistics", new AjaxOptions()))
{
@Html.DropDownListFor(m => m.SelectedUserEmail,
new SelectList(Model.UserEmails, "Value", "Text"), "Choose user",
new {@class = "form-control", id = "dropDown1"})
<p><br/></p>
@Html.DropDownListFor(m => m.SelectedDate,
new SelectList(Model.Dates, "Value", "Text"), "Choose date",
new {@class = "form-control", id = "dropDown2"})
}
@section Scripts
{
<script>
$("#dropDown2").on("change",
function() {
var date = $(this).val();
$("#dropDown1").on("change",
function() {
var user = $("#dropDown1").val();
var url = '@Url.Action("GetStatistics", "GroupStatistics")' +
"?selectedDate=" +
date +
"&selectedUserEmail=" +
user;
if (user !== undefined && date !== undefined) {
$("#partialView").load(url,
function() {
$('#partialView').fadeIn('fast');
});
}
});
})
</script>
}
控制器:
[HttpGet]
public ActionResult GetStatistics(string selectedDate, string selectedUserEmail)
{
//some actions
return PartialView("_MemberStatisticsPartial", testRunViewModels);
}
解决方案
像这样的东西
$("#dropDown2").on("change", doSomething)
$("#dropDown1").on("change", doSomething)
function doSomething() {
var user = $("#dropDown1").val();
var date = $("#dropDown2").val();
if (user !== undefined && date !== undefined) {
var url = '@Url.Action("GetStatistics", "GroupStatistics")' +
"?selectedDate=" +
date +
"&selectedUserEmail=" +
user;
$.ajax({
type: 'POST',
url: url,
async:false,
success: function (data) {
$("#partialView").html(data)
$('#partialView').fadeIn('fast');
}
});
}
}
推荐阅读
- python - 以下示例中嵌套循环的工作原理
- javascript - 在 TypeScript 中导出类和类型的最佳方法
- javascript - 如何将数据从一个 app.js 文件发送到 Routes 中的 index.ejs 文件
- javascript - 如何解决jspdf名称冲突
- sql - 关联表中的 SQL Server 汇总(总和)值
- node.js - express-handlebars 不会呈现数据,但会呈现数据的模板
- python - 合并特定数组并对它们进行排序
- mysql - 选择语句向后返回 ID
- polymer - 根据 dom-repeat 中的索引计算属性值
- javascript - react中的useForm无法正常进行两次手动更新