iOS cordova应用程序中的文本区域只接受一次输入
Textarea in iOS cordova application accepts input only once
具体来说,在安装应用程序后,可以通过软键盘输入来更改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的作用就好像前一个对象仍然存在一样。明确删除它可以解决这个问题。
- 单击其他元素或鼠标向上时隐藏输入框,但保留一次焦点
- iOS cordova应用程序中的文本区域只接受一次输入
- 当我触发一个事件时,它会被触发几次,而不是在jQuery中的输入时触发一次
- 在用户为下一次输入插入数据后清空文本字段
- 输入:焦点只工作一次
- PHP 在输入内单击一次
- 在 yii 框架上收集多个输入以进行一次验证
- 允许用户添加表单输入字段:为什么 append() 只工作一次
- 使用输入多少次掷骰子,然后只输出最后一掷Javascript上的两个骰子的总和
- 我怎样才能在“鼠标输入”和“鼠标离开时”中仅使用一次变量
- JavaScript 输入一次特定字符
- JS处理延迟后输入仅一次
- 单击一次按钮即可用随机化值填充多个输入
- 输入时只允许输入一次数字
- 文件输入jQuery更改事件只触发一次
- 即使用户一次又一次地输入同一封电子邮件,代码也不会停止提交表单
- 如何通过一次输入将项目添加到列表中
- 单击时添加一次文本进行输入
- 使用单个输入元素上传多个文件(不是多个选择,而是一次一个)
- 输入验证只工作一次