代码之家  ›  专栏  ›  技术社区  ›  Andy

在文本区域中保存文本的独立于浏览器的方式

  •  0
  • Andy  · 技术社区  · 8 年前

    使用ASP。NET MVC,我有一个包含TextAreaFor的视图,我希望用户能够在其中键入一些注释并动态保存它们,查看以前保存在那里的注释(无论是他们还是其他用户),以及修改现有注释(如添加其他注释)。这是我的。。。。

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

    $(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中,虽然它不会清空现有笔记,但仍然不会保存添加的新笔记。什么是独立于浏览器的方式,我可以使这项工作,使每个人的笔记将得到妥善保存,无论他们使用什么?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Shyju    8 年前

    您可以使用jQuery val()

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

    您也可以考虑使用Url。动作助手方法,以生成到动作方法的正确相对路径。

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