2017-09-22 63 views
0

使用ASP.NET MVC,我有一个包含TextAreaFor的View,我希望用户能够键入一些注释并将它们即时保存,请参阅那些之前保存的记录(无论是由他们还是其他用户),以及修改现有的笔记(如添加额外的笔记)。下面是我....在TextArea中保存文本的独立于浏览器的方式

在视图中的div:

<div class="form-group"> 
     <label for="InternalNotes" class="control-label">Internal Notes</label> 
     @Html.TextAreaFor(w => w.InternalNotes, new { @class = "form-control" , @id="notes" }) @*this is editable*@ 
    </div> 
    <div class="form-group"> 
     <div class="col-xs-6"> 
      <button type="button" id="savenotes" class="btn btn-default btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Save Request Notes</button> 
      <div style="color:green; display:none" id="notessuccess">Notes successfully saved</div> 
      <div style="color:red; display:none" id="noteserror">Notes unable to be saved</div> 
     </div> 
     <div class="col-xs-6 text-right"> 
      <button type="submit" id="deletereq" class="btn btn-default btn-primary" value="delete" name="submit"><span class="glyphicon glyphicon-remove"></span> Delete Request</button> 
     </div> 
    </div> 

因此,用户可以输入一些东西到TextAreaFor,然后点击“savenotes”按钮,它应该通过Ajax保存。这是jQuery的为:

$(document).ready(function() { 

    $("#savenotes").click(function() { 

     $("#notessuccess").hide(); 
     $("#noteserror").hide(); 

     var id = @Model.AccessRequestId; 
     var notes = document.getElementById("notes").textContent; //innerText; 

     $.ajax({ 
      data: { 'id': id, 'notes': notes }, 
      type: 'POST', 
      //contentType: "application/json; charset=utf-8", 
      url: '/Administration/SaveRequestNotes', 
      success: function (data) { 
       if (data.success == "ok") { 
        $("#notessuccess").fadeIn(); 
       } else { 
        $("#noteserror").fadeIn(); 
       } 
      }, 
      fail: function (data) { 
       $("#noteserror").fadeIn(); 
      } 
     }); 
    }); 
}); 

的“的innerText”被注释掉了,因为那是我最初使用,但它只能在Internet Explorer中的工作 - 另一个用户使用Chrome,在那里他可以看到其他用户的笔记已经存在,但是当他试图保存除他们之外的笔记时,它会把它全部弄出来,所以笔记将是空的!

所以我将它改为“textContent”。这仍然可以在Internet Explorer中使用,但现在在Chrome和Firefox中都可以使用,但它不会清空现有的笔记,但仍不会保存添加的笔记。什么是独立于浏览器的方式,我可以使其工作,以便每个人的笔记都能正确保存,无论他们使用什么?

谢谢!

回答

0

您可以使用jQuery的val()方法获取输入到文本区域

var notes = $("#notes").val(); 
alert(notes); 

你也可以考虑使用Url.Action辅助方法来生成你的操作方法正确的相对路径文本用户。

url: '@Url.Action("SaveRequestNotes","Administration")', 
+0

看起来像使用.val()工作。呃,我不知道为什么我以前不会这么想......我认为这会是一件愚蠢的事!只是想知道如何使用@ Url.Action比使用我现在拥有的更好? – Andarta

+0

无论您的网址如何,它都会创建正确的相对路径。 – Shyju

相关问题