与浏览器无关的方式在TextAreaFor中保存文本

时间:2017-09-22 19:08:41

标签: ajax asp.net-mvc text cross-browser textarea

使用ASP.NET MVC,我有一个包含TextAreaFor的View,我希望用户能够输入一些注释并即时保存它们,请参阅之前保存的注释(无论是否由它们保存)或其他一些用户),以及修改现有笔记(比如添加其他笔记)。这就是我拥有的......

视图中的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中键入内容,然后点击&#34; savenotes&#34;按钮,应该通过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();
            }
        });
    });
});

&#34; innerText&#34;被注释掉了,因为这是我最初使用的,但它只在Internet Explorer中工作 - 另一个用户正在使用Chrome,在那里他可以看到已经存在的其他用户注释,但当他和#39; d除了他们之外还试着保存笔记,它会把它全部吹掉所以笔记会是空的!

所以我将其改为&#34; textContent&#34;。这仍然适用于Internet Explorer,但现在在Chrome和Firefox中,虽然它不会清空现有笔记,但仍然无法保存添加的新笔记。什么是与浏览器无关的方式,我可以使这项工作,以便每个人的笔记将得到适当保存,无论他们使用什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用jQuery print('What is the term to be searched?') term = input() browser = webdriver.Firefox(executable_path ='/usr/local/bin/geckodriver') browser.get('https://google.com/search?q=' + term) try: link = browser.find_element_by_link_text("Hello") # (I want to make it so the first page has no links of interest, so I can proceed to clicking the next page) except NoSuchElementException: nextPage = browser.find_element_by_css_selector('Css of next page button) nextPage.click() 方法获取文本用户输入到textarea

Traceback (most recent call last):
  File "/Users/shaun/Documents/Python/mwpractice.py", line 17, in <module>
    nextPage = browser.find_element_by_css_selector('''<span class="csb ch" style="background:url(/images/nav_logo242_hr.png) no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>''')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 498, in find_element_by_css_selector
    return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 832, in find_element
    'value': value})['value']
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: Given css selector expression "<span class="csb ch" style="background:url(/images/nav_logo242_hr.png) no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>" is invalid: InvalidSelectorError: '<span class="csb ch" style="background:url(/images/nav_logo242_hr.png) no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>' is not a valid selector: "<span class="csb ch" style="background:url(/images/nav_logo242_hr.png) no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>"

您可能还会考虑使用Url.Action帮助程序方法生成操作方法的正确相对路径。

val()