通过JS添加textarea的价值

时间:2012-11-30 12:13:57

标签: javascript html

我不确定我是否正确行事:

 <div>
    <form id="formPost" method="post">

    <div id="testdiv">
    </div>

    </form>
 </div>
 <div class="actions-left">
    <div id="textarea">
      <a href="javascript:ShowReplyTextArea();" class="button">Reply</a>
    </div>  
    <div id="postButton">
      <a href="javascript:ReplyPost(<%: Model.News.NewsId %>);" class="button">Post</a>
    </div>
    <div class="clear"></div>
  </div>

 <script type="text/javascript">

 $(document).ready(function() {
   $("#postButton").hide();
 });

 function ShowReplyTextArea() {
   div = document.getElementById("testdiv")
   var textArea = document.createElement("textarea");
   textArea.rows = 10;
   textArea.cols = 10;
   textArea.id = "post_textarea";

   div.appendChild(textArea);

   if ($("#post_textarea").val() != null)
     $("#postButton").show();
 }

 function ReplyPost(newsId)
 {
   div = document.getElementById("testdiv")
   var textArea = document.createElement("textarea");
   div.appendChild(textArea);
   textArea.id = "reply_textarea";
   textArea.value = this.valueOf();;
   **var message = textArea.value.toString(); //problem here dont know how???
   alert(message);
   var jqxhr = $.getJSON("<%= Url.Action("ReplyPost", "Home", new { area = "News" }) %>?newsId=" + newsId + "&message=" + message, function (data) {**
   });
 }
</script>

然后在我的控制器中:

[HttpGet]
[NoCache]
public JsonResult ReplyPost(int newsId, string message)
{
  ZincService.NewsService.ReplyPost(newsId, message);
  return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}

此消息来源:

  <div>
    <form id="formPost" method="post">

    <div id="testdiv">
    </div>

    </form>
  </div>
  <div class="actions-left">
    <div id="textarea">
      <a href="javascript:ShowReplyTextArea();" class="button">Reply</a>
    </div>  
    <div id="postButton">
      <a href="javascript:ReplyPost(116);" class="button">Post</a>
    </div>
    <div class="clear"></div>
  </div>

我如何得到文本区域的值,以便我可以传递它,我现在在那里的代码是不正确的,我做的所有这一切正确?我看到我的文字没有文字区域我输入“sdfgsdfg”

感谢

1 个答案:

答案 0 :(得分:1)

只需使用:

var message = textArea.innerHTML;

或者

var message = textArea.innerText;

或者

var message = $(textArea).html();

或者

var message = $(textArea).text();