简体   繁体   English

如何知道某些文本已输入以及光标已从文本框中移出?

[英]How to know that some text is entered as well as cursor has come out of textbox?

I want to know if there is any event or way to know that some text has been entered into the textarea and also the user has come of the textarea, so that i can invoke some text like "success". 我想知道是否有任何事件或方式知道某些文本已输入到文本区域中,并且用户也来自文本区域,以便我可以调用“成功”之类的文本。

For the some values in the textarea i think we can do through val() function. 对于textarea中的一些值,我认为我们可以通过val()函数来完成。 But how to know that user has come out of the Textarea 但是如何知道用户已经离开了Textarea

My code is like: 我的代码是这样的:

<input type="textarea" id="link1"></textarea>
if (!$("#link1").val()) {
    // textarea is empty
}

You can also use $.trim to make sure the element doesn't contain only white-space: 您还可以使用$ .trim来确保该元素不只包含空格:

if (!$.trim($("#link1").val())) {
    // textarea is empty or contains only white-space
}

and for detecting when user comes out of textarea 以及检测用户何时离开文本区域

$('#link1').focusout(function() {
    alert(this.id + " focus out");
});

尝试捕获onblur事件,然后检查value.length以查看是否在文本框中添加了一个值。

textarea should be like textarea应该像

<textarea id="link1"></textarea>

events you are searching are onkeyup , onfocus and onblur 您正在搜索的事件是onkeyuponfocusonblur

使用onKeyPress可以检测到输入了一些文本,而onBlur可以检测到用户来自文本区域

<input type="textarea" id="link1" onBlur = blured() onKeyPress = pressed()></textarea>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM