添加文字和评论功能更新Div
Updating the Div with added text, comment feature
我正在开发一个使用剃刀语法的MVC应用程序。
我正在开发用于注释功能的分部类。
我有如下模式显示注释输出的代码。
John Smith 15-Aug-2012 ------------------------------- Called Customer today, hold me to call later. Will Parkar 15-Aug-2012 ------------------------------- Keep track with him. *Add New Comment in below text box.* ___________________________________________ |Called Again... | | | |___________________________________________| Add Comment Clear
现在,每当用户将注释放在文本框中时,该文本都应该添加到上面的列表中。。。
输出应该是
John Smith 15-Aug-2012 ------------------------------- Called Customer today, hold me to call later. Will Parkar 15-Aug-2012 ------------------------------- Keep track with him. John Smith 16-Aug-2012 ------------------------------- Called Again... <---------------------New Comment get added here. *Add New Comment in below text box.* ___________________________________________ | | | | |___________________________________________| Add Comment Clear
我有以下代码。。。
@model IEnumerable<CRMEntities.Comment>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<!DOCTYPE html>
<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
function clearText()
{
document.getElementById('Comment').value = "";
}
</script>
<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$('#AddCommentButton').click(function () {
$.ajax({
type:'post',
url: '/Comment/SaveComments', //url to your action method
dataType: 'json',
data: { 'comments': $('#Comment').val() },
success: function(data)
{
$('#ParentBlock').appendChild("<div>" + data.msg + "</div>");
}
});
});
</script>
<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".ShowComments").click(function () {
$(".ParentBlock").slideToggle("slow");
$("CommentP").append(document.getElementById('Comment').value);
});
});
</script>
<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".ShowComments2").click(function () {
$(".1").append("<strong>Hello</strong>");
});
});
</script>
<style type="text/css">
div.ParentBlock
{
position:relative;
display:none;
}
#ClassPara
{
position:relative;
background-color:#ECF5FC;
cursor:pointer;
border:2px;
width: 115px;
border-style:solid;
border-width:thin;
border-color: #DCEDF8;
}
<style type="text/css">
#OwnerName
{
background-color : #F0F6FF;
font-style:normal;
font-family:Calibri;
}
#CommentTextBlock
{
background-color : #F9F9FF;
}
#EmpName
{
font-style:normal;
font-size:medium;
}
#Clear
{
text-decoration:underline;
cursor:pointer;
color:Blue;
}
#AddComment
{
text-decoration:underline;
cursor:pointer;
color:Blue;
}
</style>
</head>
<body>
@{
<p id="ClassPara" class="ShowComments" >Show Comments</p>
<div class="ParentBlock">
@foreach (var item in Model)
{
<div id="OwnerName">
<span class="EmpName"> @Html.ActionLink(item.Owner.FullName, "Details", "EMployee", new { id = item.OwnerId }, new { @style = "color:#1A6690;" })</span>
@Html.DisplayFor(ModelItem => item.CommentDateTime)
</div>
@* <div id="CommentTextBlock">
@Html.DisplayFor(ModelItem => item.CommentText)
</div>*@
<p class="CommentP">
@Html.DisplayFor(ModelItem => item.CommentText)
</p>
<br />
}
</div>
@Html.TextArea("Comment", "", 5, 80, "asdsd")
<input type="button" value="Add Comment" id="AddCommentButton"/>
<input type="button" value="Clear" onclick="clearText()"/>
<br />
@* <label id="AddComment">Add Comment</label>
<label id="Clear" onclick="clearText()">Clear</label>*@
}
</body>
</html>
如何做到这一点?
单击ADD Comment
按钮,将该注释发布到您的操作中,以将其保存到数据库或您想要保存的任何位置,然后在ajax
的回调函数中返回该注释以在页面上显示。
$('#addCommentButtonID').click( function() {
$.ajax({
type:'post',
url: 'SaveComments' //url to your action method
dataType: 'json',
data: {'comments':$('#textboxId').val()},
success: function(data)
{
$('#yourMainDiv').appendChild("<div>"+data.msg+"</div>");
}
});
});
第二种方式:
$('#addCommentButtonID').click( function() {
$.post('SaveComments',comments:$('#commentTextbox').val(),
function (data) {
$('#yourMainDiv').appendChild("<div>"+data.msg+"</div>");
},'json');
});
您的行动
public JsonResult SaveComments(string comments)
{
// save it wherever you want
// after saving success return this string as jsonresult
return Json(new { sc = true, msg = comment });
}
相关文章:
- 添加文字和评论功能更新Div
- 在Rails中更新Div,而不更改更新请求后的视图
- 根据CHECKBOX输入值动态更新DIV信息
- JavaScript on点击更新Div
- 使用不同的API参数更新DIV内容
- 从一个正在运行的PHP代码中,了解如何更新DIV标记中的状态
- 使用2个表单的SQL查询更新DIV
- Rails 在 JavaScript 调用后更新 DIV(从模型重新加载)
- 单击时更新 DIV 内容,而无需直接知道其 ID
- 可观测集合发生变化时更新Div
- 更新DIV的泛型函数
- 点击使用jQuery用HTML更新DIV
- 在jQuery上更新Div隐藏字段可排序拖放更新
- 如何更新DIV的内容
- 更新Div内文本的问题
- 使用来自ASP的数据更新DIV内容.NET上的事件
- 如何更新Div内的文本
- 更新DIV并在1秒后使用setTimeout再次清空它
- 基于顶部位置更新Div的类名
- 更新Div/PartialView上的Interval