单击列表框,用javascript在textarea中附加所选项目名称

单击列表框,用javascript在textarea中附加所选项目名称,javascript,html,c#-4.0,asp.net-mvc-4,listbox,Javascript,Html,C# 4.0,Asp.net Mvc 4,Listbox,使用:ASP.Net MVC 4.0,VS2010,.netfw 4.0 我的控制器包括: ViewBag.HtmlContent = model.Content; ViewBag.list = model.TableColumn; try { HtmlString hs = new HtmlString(model.Content); List<string> lstI

使用:ASP.Net MVC 4.0VS2010.netfw 4.0

我的控制器包括:

        ViewBag.HtmlContent = model.Content;
        ViewBag.list = model.TableColumn;

        try
        {
            HtmlString hs = new HtmlString(model.Content);
            List<string> lstItems = new List<string>();
            //IEnumerable<string> lst = new IEnumerable<string>();
            string queryToGetAllTable = "select column_name,* from information_schema.columns where table_name = 'BAP_AXA_IN' order by ordinal_position ";
            DataTable dt = CS.Return_DataTable(queryToGetAllTable);
            foreach(DataRow dr in dt.Rows)
            {
                lstItems.Add(dr["COLUMN_NAME"].ToString());
            }
            model.TableColumn = new List<string>();
            foreach( string  str in lstItems)
                model.TableColumn.Add(str);
        }

        catch { }
        return View(model);
ViewBag.HtmlContent=model.Content;
ViewBag.list=model.TableColumn;
尝试
{
HtmlString hs=新的HtmlString(model.Content);
列表项=新列表();
//IEnumerable lst=新的IEnumerable();
string queryToGetAllTable=“从信息_schema.columns中选择列_name,*,其中表_name='BAP_AXA_IN'按顺序_位置排序”;
DataTable dt=CS.Return\u DataTable(queryToGetAllTable);
foreach(数据行dr在dt.行中)
{
添加(dr[“COLUMN_NAME”].ToString());
}
model.TableColumn=新列表();
foreach(lstItems中的字符串str)
model.TableColumn.Add(str);
}
捕获{}
返回视图(模型);
我的观点类:

  @model AboutModel
@using MvcApplication1.Models

@{
    ViewBag.Title = "BAP Automation";
}

@using (Html.BeginForm()) {

    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Create Mail</legend>
    <div class="editor-label">
        @Html.LabelFor(model => model.CompanyName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.CompanyName)
        @Html.ValidationMessageFor(model => model.CompanyName)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.MailSubject)
    </div>

    <div class="editor-field">
        @Html.EditorFor(model => model.MailSubject)
        @Html.ValidationMessageFor(model => model.MailSubject)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.MailBody)
    </div>

    <div class="editor-field">
        @Html.EditorFor(model => model.MailBody)
        @Html.ValidationMessageFor(model => model.MailBody)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.TableColumn)
    </div>

    <div class="editor-list-field">
        @Html.ListBoxFor(model => model.TableColumn, new SelectList(Model.TableColumn), new { @class = "listofcolumn"}))
        @Html.ValidationMessageFor(model => model.TableColumn)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Content)
    </div>

    <div class="editor-multiline-field">
        @Html.TextAreaFor(model => model.Content, new { cols=60,@rows=10, @class = "textarea"})
        @Html.ValidationMessageFor(model => model.Content)
    </div>

        <p>
            <input type="submit" value="Create" />
        </p>

        <p>
            Posted Content : @ViewBag.HtmlContent
        </p>

    </fieldset>
}
<script type="text/javascript">
$(function() {
    ​$('.listofcolumn option')​.click(function() {
        if ($(this).is(':selected')) {
            var selectedId = $(this).val();
            var selectedText = $(this).text();
            $('.textarea').val(selectedText);
        }
    });​
});
</script>
@model关于模型
@使用mvcapapplication1.Models
@{
ViewBag.Title=“BAP自动化”;
}
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
创建邮件
@LabelFor(model=>model.CompanyName)
@EditorFor(model=>model.CompanyName)
@Html.ValidationMessageFor(model=>model.CompanyName)
@LabelFor(model=>model.MailSubject)
@EditorFor(model=>model.MailSubject)
@Html.ValidationMessageFor(model=>model.MailSubject)
@LabelFor(model=>model.MailBody)
@EditorFor(model=>model.MailBody)
@Html.ValidationMessageFor(model=>model.MailBody)
@LabelFor(model=>model.TableColumn)
@Html.ListBoxFor(model=>model.TableColumn,新建SelectList(model.TableColumn),新建{@class=“listocolumn”}))
@Html.ValidationMessageFor(model=>model.TableColumn)
@LabelFor(model=>model.Content)
@Html.TextAreaFor(model=>model.Content,new{cols=60,@rows=10,@class=“textarea”})
@Html.ValidationMessageFor(model=>model.Content)

发布内容:@ViewBag.HtmlContent

} $(函数(){ ​$(“.listofcolumn选项”)​.单击(函数(){ 如果($(this).is(':selected')){ var selectedId=$(this.val(); var selectedText=$(this.text(); $('.textarea').val(selectedText); } });​ });
“当我点击列表框(class=listocolumn)时,html.textareafor(class-textarea)将被填充/textarea+=所选列表项”


但是运行时会显示以下错误:Microsoft JScript运行时错误:需要对象

您可以通过在
change
事件上运行jQuery并获取当前值来简化jQuery。它还应该解决对象未找到的问题。试试这个:

​$('.listofcolumn')​.change(function() {
    var selectedId = $(this).val();
    var selectedText = $('option:selected', this).text();
    $('.textarea').val($('.textarea').val() + ' ' + selectedText);
});​