开发者

Detect a keyword entered in a textarea when user clicks submit on a form

using jQuery, is it possible to detect if a keyword (or multiple keywords) has been entered in a textarea when a user clicks submit on a form?

Say I type, "This text has a keyword in it." and click submit in the form, is it possible to display a simple aler开发者_如何学Got that tells me that I typed this particular word?

Thanks for any info you can give me.


yes it's easy

var text = $('textarea#mytextarea').val(); //gets the text from textarea user your own id
if (text.indexOf("keyword") >= 0)
  alert('keyword');

EDIT: you have to put this code to be triggered by the submit button. if this is your form

<form id="target" action="destination.html">
<input type="text" value="Hello there" />
<input type="submit" value="Go" />
</form>

then you can trigger it by doing this

$('#target').submit(function() {
  var text = $('textarea#mytextarea').val(); //gets the text from textarea user your own id
  if (text.indexOf("keyword") >= 0)
    alert('keyword');
  return false;
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜