根据DDL选定值自动填充TextArea

时间:2016-05-25 17:58:58

标签: javascript jquery razor

我有 DDL &和 TextArea

<div class="form-group">
    @Html.LabelFor(model => model.activityID, "Assignment", htmlAttributes: new { @class = "control-label col-md-2 required" })
    <div class="col-md-10">
        @Html.DropDownList("activityID", null, "-- Select Activity --", htmlAttributes: new { @id = "activityID", @class = "form-control" })
        @Html.ValidationMessageFor(model => model.activityID, null, new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Summary, htmlAttributes: new { @class = "control-label col-md-2 required" })
    <div class="col-md-10">
        @Html.TextAreaFor(model => model.Summary, 15, 50, new { htmlAttributes = new { @id = "Summary-Textbox", @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Summary, "", new { @class = "text-danger" })
    </div>
</div>
基于此填充

activityID ..

ViewBag.activityID = AMethods.lstDailySumAssignments();

AMethods.lstDailySumAssignments()就是这样:

public static List<SelectListItem> lstDailySumAssignments()
{
    List<SelectListItem> lstAssignments = new List<SelectListItem>();

    using(var context = new ALogEntities())
    {
        List<codeAC> lstActivity = context.codeAC.Where(x => x.deleted == false).ToList();
        var groups = lstActivity.GroupBy(x => x.codeACatagory.Text);
        foreach(var group in groups)
        {
            var slg = new SelectListGroup() { Name = group.Key };

            foreach(codeAC activity in group)
            {
                SelectListItem item = new SelectListItem() { Text = activity.text, Value = activity.ID.ToString(), Group = slg };
                lstAssignments.Add(item);
            }
        }

    }

    return lstAssignments;
}

现在我想要发生的是,当选择特定类别下的特定选项时,要自动填充的文本区域例如

This Test Worked!
Great Job!

我在JQuery中有这个:

$(document).ready(function () {
    $('#activityID').change(function () {
        var selectedActivity = this.options[this.selectedIndex].parentNode.label;
        if (selectedActivity === "DEMONSTRATIONS") {
            $('#Summary-Textbox').value = "This Test Worked! \n Great Job!";
        }
        else if (selectedActivity !== "DEMONSTRATIONS") {
            $('#Summary-Textbox').value = "";
        }
    });
});

感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

我明白了。我必须像这样更改valueval()

$(document).ready(function () {
$('#activityID').change(function () {
    var selectedActivity = this.options[this.selectedIndex].parentNode.label;
    if (selectedActivity === "DEMONSTRATIONS") {
        $('#Summary-Textbox').val("This Test Worked! \nGreat Job!");
    }
    else if (selectedActivity !== "DEMONSTRATIONS") {
        $('#Summary-Textbox').val('');
    }
});
});