Javascript 从MVC中的下拉列表选择填充文本框
在从dropdownlist中选择一个选项后,我试图从jquery中填充一个文本框。我包含了一些代码片段 这是视图的标记Javascript 从MVC中的下拉列表选择填充文本框,javascript,c#,asp.net-mvc,Javascript,C#,Asp.net Mvc,在从dropdownlist中选择一个选项后,我试图从jquery中填充一个文本框。我包含了一些代码片段 这是视图的标记 <div class="form-group"> @Html.LabelFor(model => model.DependantID, "DependantID", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-
<div class="form-group">
@Html.LabelFor(model => model.DependantID, "DependantID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DependantID", null, htmlAttributes: new { @class = "form-control", @onchange = "GetRegNo()" })
@Html.ValidationMessageFor(model => model.DependantID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RegNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.RegNo, new { htmlAttributes = new { @class = "form-control" } })*@
<span id="loading_progress1" style="display: none;">Please wait...</span>
@Html.TextBoxFor(model => model.RegNo, new { @Value = @ViewBag.Regno, @readonly = "readonly", @class = "form-control" })
@*@Html.TextBoxFor(model => model.RegNo, new { @Value = Regno, @readonly = "readonly", @class = "form-control" })*@
@Html.ValidationMessageFor(model => model.RegNo, "", new { @class = "text-danger" })
</div>
</div>
因此,从我上面的代码片段来看,当dropdownlist中发生更改时,会调用java,java反过来调用控制器中的方法并填充文本框。如果有更好的办法,我完全愿意。谢谢。我发现jquery脚本存在问题。我将张贴更改并指向更改。这和我的作业有关。我在这里标记的行将值发布到文本框中
function GetRegNo() {
var dependid = $('#DependantID').val();
var dprogress = $('#loading_progress1');
dprogress.show();
//alert(schoolid);
$.ajax({
cache: false,
//url: '@Url.Action("/Patients/FillClass")',
url: '/Patients/FillClass',
type: "GET",
datatype: "json",
data: { DependID: dependid },
success: function (stud) {
$("#RegNo").html(""); ///clear entry before appending
here: $('input#RegNo').val(stud); //i missed it here, changing to this changed solved the problem, compare with with script in the question
dprogress.hide();
},
error: function (response) {
alert("Error: " + response.responseText);
dprogress.hide();
}
});
}
你发了一堆代码。它不起作用吗?您的问题中没有明确的问题状态。另外,你不应该把“JavaScript”称为“Java”,因为它们完全不同。谢谢你对Java和JavaScript的更正,我的错。但是我发布代码是为了让人们看到我正在努力实现的目标。不,它不起作用!同样,您没有在问题中提供明确的问题陈述。我不知道“不工作”是什么意思。你需要具体地描述什么是不起作用的。我想执行一种级联下拉列表,但不是填充另一个下拉列表,而是填充一个文本框。希望这能解释我想要实现的目标?
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult FillClass(int? dependid)
{
if (dependid == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
string regno="";
var N = db.Patients.Max(f => (int?)f.PatientID).GetValueOrDefault(0);
N++;
string Nt = Convert.ToString(N);
if (dependid == 1)
{
regno = "M" + Convert.ToString((DateTime.Now).ToString("yy")) + BizLogic.Right("00000" + Nt, 5);
}
else
{
regno = "D" + Convert.ToString((DateTime.Now).ToString("yy")) + BizLogic.Right("00000" + Nt, 5);
}
return Json(regno, JsonRequestBehavior.AllowGet);
}
function GetRegNo() {
var dependid = $('#DependantID').val();
var dprogress = $('#loading_progress1');
dprogress.show();
//alert(schoolid);
$.ajax({
cache: false,
//url: '@Url.Action("/Patients/FillClass")',
url: '/Patients/FillClass',
type: "GET",
datatype: "json",
data: { DependID: dependid },
success: function (stud) {
$("#RegNo").html(""); ///clear entry before appending
here: $('input#RegNo').val(stud); //i missed it here, changing to this changed solved the problem, compare with with script in the question
dprogress.hide();
},
error: function (response) {
alert("Error: " + response.responseText);
dprogress.hide();
}
});
}