简体   繁体   中英

How to display an error if the content of a <textarea></textarea> is empty using javascript?

如果用户在填写表单时将必填的输入字段留空,我知道如何显示错误消息,但是我无法对textarea进行同样的操作。

i think on change fires for text areas as well so assuming it does

theTextArea.onChange = function(){
  if(this.value == ''){
     alert('You must supply a value for this field');
  }
};

or with jquery

$(document).ready(
  function(){
    $('#mytextarea').change(function(){
      var $this = $(this);
      if($this.val() == ''){
         alert('You must supply a value for this '+$("label[for='mytextarea']").text());
      }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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