2013-01-14 30 views
0

使用:ASP.Net MVC 4.0VS2010.netfw 4.0點擊列表框在textarea的追加選擇項目名稱與JavaScript

我控制器包括:

 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); 

我查看分類:

@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> 

「當我點擊on listbox(class = listofcolumn) html.textareafor(class textarea)將被填充/ textarea + =選擇列表項「。

但跟蹤誤差顯示在運行時:微軟JScript運行時錯誤:預期的對象

回答

0

您可以通過運行其上的change事件,並獲取當前值簡化您的jQuery。它也應該解決找不到物體的問題。試試這個:

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