是否可以对在 JQuery 中键入的文本区域执行实时检测
Is it possible to perform live detection of text typed into textarea in JQuery?
我有一个"发布"按钮,当文本输入文本时,它会激活(从模糊到颜色,从禁用到启用)。
这工作正常,但我希望文本区域在文本区域中没有任何内容时检测到。所以假设我在文本区域中输入了"abc"。提交按钮将变为活动状态,但假设我现在删除了所有文本。按钮仍处于活动状态。好吧,不完全,因为我有这个:
commentButton.prop({ disabled: commentBox.val() == 0 });
这将阻止表单提交,文本区域中没有任何内容。问题是提交按钮仍然是彩色的,并且不会像预期的那样返回到模糊的非活动状态。
所以这让我想到了我的问题。
我可以设置一些东西,一旦我专注于文本区域并键入并知道何时从文本区域删除了所有内容,就会激活并开始收听吗?
这样,我可以设置一个 if 语句来说明文本是否全部删除,然后删除彩色状态按钮的 moveClass 和模糊状态按钮的 addClass。
这也应该适用于空格。
这是我当前的代码:
.comment_box = 我的文本区域
.activeCommentButton = 彩色提交按钮
.commentButton = 模糊的提交按钮
$(".microposts").off().on("focus", ".comment_box", function () {
$(this).prop("rows", 7);
$(this).parent().find(".activeCommentButton").removeClass("activeCommentButton").addClass("commentButton");
var commentBox = $(this),
form = $(this).parent(),
cancelButton = form.children(".commentButtons").children(".cancelButton"),
commentButton = form.children(".commentButtons").children(".commentButton");
$(this).removeClass("comment_box").addClass("comment_box_focused").autoResize();
form.children(".commentButtons").addClass("displayButtons");
commentButton.prop({
disabled: true
});
commentBox.off().on("keyup keypress input change", function () {
commentButton.prop({
disabled: commentBox.val() == 0
});
commentButton.removeClass("commentButton").addClass("activeCommentButton");
});
cancelButton.click(function () {
commentBox.removeClass("comment_box_focused").addClass("comment_box");
form.children(".commentButtons").removeClass("displayButtons");
commentBox.val("");
});
});
亲切问候
是的,试试这个,你可以修改它:
$(".commentButton").prop({ disabled: true });
$(".comment_box").keypress(function() {
$(".commentButton").prop({ disabled: !$(this).val().length >= 1 })
});
这对我有用,但有没有更清洁的方法?
commentButton.prop({ disabled: true });
commentBox.off().on("keyup keypress input change", function () {
commentButton.prop({ disabled: commentBox.val().length == 0 });
if ( commentBox.val().length > 1 ) {
commentButton.removeClass("commentButton").addClass("activeCommentButton");
} else {
commentButton.removeClass("activeCommentButton").addClass("commentButton");
}
});
相关文章:
- onkeyup无法动态创建多个文本区域
- 具有所有样式的文本正在复制到可编辑文本区域
- 在文本区域POST后解码JSON
- 在文本区域中使用jQuery.text()保持换行符
- ng在下拉列表和文本区域提交
- 将文本插入光标所在的文本区域
- 离开页面导航后保留文本区域内容
- Javascript-在文本区域中断,但不在段落中中断
- 当选择值x时,Javascript需要在正确的位置显示文本区域,从而循环通过具有选择选项的表单
- 测试文本区域中的特定文本格式
- 文本编辑后,append函数不适用于文本区域
- 如何在ReactJs中链接下拉列表和文本区域
- I'我试图在文本区域中进行特定的输入,调用特定的javascript函数,但没有成功;不起作用
- 使用JSON文件中的变量(字符串)填充文本区域
- JavaScript-如果以前不存在文本,如何从文本区域删除新行
- 如何在php中创建一个函数,该函数与文本区域一起工作,通过输入类似[color:red]的内容来打印具有等效颜色的文本
- 如何为动态创建的文本区域中输入的值更新ng模型
- 延迟高亮显示文本区域中的文本
- 将字母添加到文本区域的末尾
- TinyMCE在新添加的文本区域