点击列表框,用javascript在textarea中添加选中的项目名称
Clicking on listbox append the selected item name in textarea with javascript
USING: ASP。净MVC 4.0 、 VS2010 , .netfw 4.0
my controller包括:
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);
my view class:
@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>
"当我点击listbox(class = listofcolumn)的html。textarea(类textarea)将被填充/textarea +=选中的列表项"。
Microsoft JScript运行时错误:Object expected您可以通过在change
事件上运行它并获取当前值来简化jQuery。它还应该解决未找到对象的问题。试试这个:
$('.listofcolumn').change(function() {
var selectedId = $(this).val();
var selectedText = $('option:selected', this).text();
$('.textarea').val($('.textarea').val() + ' ' + selectedText);
});
相关文章:
- 在Javascript中,当使用array.prush方法向列表中添加项目时,如何对其进行排序
- 在不向上滚动的情况下向列表框中添加项目
- 为图像滑块jquery添加项目符号功能
- 在Javascript中使用JQueryMobile向列表添加项目
- ng只重复添加项目一次
- jCarousel-动态添加项目
- 如何在控制器对添加项目执行操作后更新视图
- SharePoint 2013 在添加项目上运行 Javascript
- 从选择框中删除项目或向选择框添加项目
- 如何在 ExtJS5 中网格编辑器组合的当前记录中添加项目
- 反应多个组件添加项目
- 如何在KeystoneJS中以编程方式从页面添加项目
- 正在添加项目id's到js-if语句
- Rails-如何在添加项目时强制刷新页面
- AngularJS+simpleCart问题显示购物车内容,直到添加项目
- 按$index添加项目的角度过滤器
- OwlCarousel 2.0.0-beta.2.4-可以预先添加项目,但不能使用'add.wowl.carou
- 允许使用向asp.net mvc4模型添加项目
- 在Javascript中按下按钮时向列表中添加项目
- contentflow通过函数添加项目