iOS cordova应用程序中的文本区域只接受一次输入

Textarea in iOS cordova application accepts input only once

本文关键字:输入 一次 应用程序 cordova 区域 文本 iOS      更新时间:2024-02-07

具体来说,在安装应用程序后,可以通过软键盘输入来更改textarea值一次。这是一个单页应用程序,我使用Handlebars进行模板化。这是模板:

<script id="comment-form-tpl" type="text/template">
        <header class="bar bar-nav">
          <a href="#" class="btn btn-link btn-nav pull-left">
            Cancel
          </a>
          <h1 class="title">Add Comment</h1>
        </header>
        <div class="bar bar-standard bar-footer">
          <button type="button" id="save-comment" class="btn btn-block">Save</button>
        </div>
        <div class="content">
          <form class="input-group">
            <textarea id="comment-text" placeholder="Comment" rows="10" autofocus>{{{comment}}}</textarea> 
          </form>
        </div>
    </script>

我使用附加在保存按钮上的javascript从文本区域获得输入:

var CommentAddView = function() {
var Comment = new Object();
this.initialize = function() {
    this.$el = $('<div/>');
    this.$el.on('click', '#save-comment', this.saveComment);
    if (window.localStorage.getItem("comment")) {
        var text = window.localStorage.getItem("comment");
        text = text.replace(/<br>/g, "'n");
        this.setComment(text);
    }
    this.render();
};
this.render = function() {
    this.$el.html(this.template(Comment));
    return this;
};
this.setComment = function(text) {
    Comment.comment = text;
}; 
this.saveComment = function() {
    var text = $("#comment-text").val();
    text = text.replace(/('r'n|'r|'n)/g, '<br>');
    window.localStorage.setItem("comment", text);
    router.load('');
};
this.initialize();

};

这一切都能完美地工作一次——文本区域显示在软键盘上,我可以键入一些内容,然后保存到本地存储中。然而,编辑是不起作用的:正如你所看到的,如果本地存储中有一个"注释"值,它就会被放在文本区域。这是有效的,但值不能更改。我可以使用软键盘输入新的文本,它会显示在文本区域,但当我触摸"保存"时,我可以通过登录控制台来验证$("#comment-text").val()没有更改。与尝试删除类似:删除会从本地存储中删除"comment"的值。如果我尝试输入新的注释,文本区域将显示为空白,我可以输入新的评论,但当我尝试保存时,$("#comment-text").val()的值是第一个应该删除的注释!如果我试着通过放线来解决这个问题$("#comment-text").val("");则CCD_ 4确实是空的。。。但是它仍然不能通过键盘输入来改变。如果我只是在桌面上的Safari中打开应用程序,一切都能正常工作;所以这是一个特定的iOS(或者可能只是iPhone)问题。我正在iOS 8.1.3上进行测试。我在谷歌上搜索了一下,尝试了所有看起来可能有远程关联的东西,但我没有主意。有人知道这里发生了什么吗?

终于明白了!我在saveComment函数中添加了一行:$("#comment-text").remove();。因此:

this.saveComment = function() {
    var text = $("#comment-text").val();
    $("#comment-text").remove();
    text = text.replace(/('r'n|'r|'n)/g, '<br>');
    window.localStorage.setItem("comment", text);
    router.load('');
};

每次显示注释表单时,我都会创建一个新的CommentAddView对象,但我最终意识到jQuery的作用就好像前一个对象仍然存在一样。明确删除它可以解决这个问题。