jquery - 使用 jQuery 和 AJAX 使用 ASP.NET MVC 5 从数据库中的许多文本框中的下拉选择值
问题描述
我在初始加载时从下拉列表中看到许多 texbox 时遇到问题,它只加载一个文本框,但另一个没有工作,由数据库更改填充的下拉列表我使用 ajax 和 jQuery 和 ASP.NET MVC 5。
好吧,这是代码下拉列表
@Html.DropDownListFor(model => model.Rang, ViewBag.listRangs as SelectList, "إختر الرتبة", htmlAttributes : new { @class = "form-control", @id = "drop", onchange = "getValue()" })
和文本框
<div class="form-group">
@Html.LabelFor(model => model.Cours, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Cours, new { htmlAttributes = new { @class = "form-control", id = "cour" } })
@Html.ValidationMessageFor(model => model.Cours, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Tds, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Tds, new { htmlAttributes = new { @class = "form-control", id = "td" } })
@Html.ValidationMessageFor(model => model.Tds, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Tps, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Tps, new { htmlAttributes = new { @class = "form-control", id = "tp" } })
@Html.ValidationMessageFor(model => model.Tps, "", new { @class = "text-danger" })
</div>
</div>
和函数onchange
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "GET",
url: "/Coordonnees/GetData",
data: "{}",
success: function (data) {
var s = '<option value="-1">إختر الرتبة</>';
for (var i = 0; i < data.length; i++) {
s += '<option value="' + data[i].Cour + '">' + data[i].Nom + '</option>';
s += '<option value="' + data[i].td + '">' + data[i].Nom + '</option>';
s += '<option value="' + data[i].tp + '">' + data[i].Nom + '</option>';
}
$("#drop").html(s);
}
});
});
function getValue() {
var cVal = $("#drop").val();
$("#cour").val(cVal);
}
function getValue() {
var dVal = $("#drop").val();
$("#td").val(dVal);
}
function getValue() {
var pVal = $("#drop").val();
$("#tp").val(pVal);
}
控制器动作方法:
public ActionResult GetData()
{
var data = from als in db.Ranglists select new { als.Nom, als.Cour, als.td, als.tp };
return Json(data.ToList(), JsonRequestBehavior.AllowGet);
}
解决方案
您定义了 3 个同名getValue()
函数...,因此将通过提升onchange
调用最后一个函数。
将它们集成为一个:
function getValue() {
var cVal = $("#drop").val();
$("#cour").val(cVal);
$("#td").val(cVal);
$("#tp").val(cVal);
}
推荐阅读
- python - 检查我的元组元组并更改给定值
- c# - 位图的使用和处置(在不久的将来)
- sass - 在 SASS 中,有没有办法声明引用块父级的子类?
- excel - 如何查看类模块中 TextBox 的名称?
- discord.js - 如何在一定数量的违规后踢 discord.js
- reactjs - 未使用 React 和 Electron 调用事件处理程序
- java - 包 com.google.android.gms.samples.wallet 不存在导入 com.google.android.gms.samples.wallet.Constants
- python - 看到物体时游戏变慢 pygame
- javascript - JavaScript 检查时间
- javascript - 如何在 Material UI 中为卡片内容设置固定高度?我应该使用 CSS 吗?