Javascript 从Java脚本设置时,文本区域未触发值更改事件

Javascript 从Java脚本设置时,文本区域未触发值更改事件,javascript,jquery,textarea,Javascript,Jquery,Textarea,我试图在Java脚本预先填充的文本区域中添加一些CSS类。但是,即使在触发触发器(输入)事件之后,我仍然无法验证和设置css类 代码一直转到“值未设置”模式。当我尝试打印值时,它是空的。如何倾听值并在其中进行验证 JS: $(文档).ready(函数(){ document.getElementById('text\u area').value='这可以是长文本,但不是自动展开的?它应该是自动展开的。这可以是长文本,但不是自动展开的?它应该是自动展开的。这可以是长文本,但不是自动展开的?它应该是

我试图在Java脚本预先填充的文本区域中添加一些CSS类。但是,即使在触发触发器(输入)事件之后,我仍然无法验证和设置css类

代码一直转到“值未设置”模式。当我尝试打印值时,它是空的。如何倾听值并在其中进行验证

JS:

$(文档).ready(函数(){
document.getElementById('text\u area').value='这可以是长文本,但不是自动展开的?它应该是自动展开的。这可以是长文本,但不是自动展开的?它应该是自动展开的。这可以是长文本,但不是自动展开的?它应该是自动展开的。这可以是长文本,但不是自动展开的?它应该是自动展开的。';
$('textarea')。触发器('input');
$(“[autofill]”)。每个(函数(){
警报($(this.attr('autofill'),this);
if($(this.attr('autofill')==“on”){
var value=$(this).val()?true:false;
如果(值){
警报(“值集”);
}否则{
警报(“未设置值”);
}
}
});
HTML:


文本区
JSBIN链接


要将值放入
textarea
元素中,请执行以下操作:

document.getElementById('text_area').value = 'This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.';
var value = $(this).val() ? true : false;
这样做:

document.getElementById('text_area').innerHTML= 'This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.';
var value = $(this).html() ? true : false;
$(document).ready(function() {

      document.getElementById('email').value = 'bala@mindlens.com.sg';
      document.getElementById('text_area1').innerHTML = '1234This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.';

      $('textarea').trigger('input');
      $('textfield').trigger('change');


      $('[autofill]').each(function() {
        console.log($(this).attr('autofill'), this);
        if ($(this).attr('autofill') == "on") {
          var value = ($(this).html() !== "") ? true : false;
          alert("value = "+$(this).html());
          console.log("value", value);
          if (value) {
            console.log("value set");alert("value set");
          } else {
            console.log("value not set");alert("value not set");
          }
        }
  });
});
并检索textarea的值,而不是此值:

document.getElementById('text_area').value = 'This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.';
var value = $(this).val() ? true : false;
这样做:

document.getElementById('text_area').innerHTML= 'This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.';
var value = $(this).html() ? true : false;
$(document).ready(function() {

      document.getElementById('email').value = 'bala@mindlens.com.sg';
      document.getElementById('text_area1').innerHTML = '1234This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.';

      $('textarea').trigger('input');
      $('textfield').trigger('change');


      $('[autofill]').each(function() {
        console.log($(this).attr('autofill'), this);
        if ($(this).attr('autofill') == "on") {
          var value = ($(this).html() !== "") ? true : false;
          alert("value = "+$(this).html());
          console.log("value", value);
          if (value) {
            console.log("value set");alert("value set");
          } else {
            console.log("value not set");alert("value not set");
          }
        }
  });
});
编辑:

document.getElementById('text_area').innerHTML= 'This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.';
var value = $(this).html() ? true : false;
$(document).ready(function() {

      document.getElementById('email').value = 'bala@mindlens.com.sg';
      document.getElementById('text_area1').innerHTML = '1234This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.';

      $('textarea').trigger('input');
      $('textfield').trigger('change');


      $('[autofill]').each(function() {
        console.log($(this).attr('autofill'), this);
        if ($(this).attr('autofill') == "on") {
          var value = ($(this).html() !== "") ? true : false;
          alert("value = "+$(this).html());
          console.log("value", value);
          if (value) {
            console.log("value set");alert("value set");
          } else {
            console.log("value not set");alert("value not set");
          }
        }
  });
});
我有。代码如下:

JS:

document.getElementById('text_area').innerHTML= 'This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.';
var value = $(this).html() ? true : false;
$(document).ready(function() {

      document.getElementById('email').value = 'bala@mindlens.com.sg';
      document.getElementById('text_area1').innerHTML = '1234This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.This can be a long text and it is not auto expanding? it shoudl auto expand.';

      $('textarea').trigger('input');
      $('textfield').trigger('change');


      $('[autofill]').each(function() {
        console.log($(this).attr('autofill'), this);
        if ($(this).attr('autofill') == "on") {
          var value = ($(this).html() !== "") ? true : false;
          alert("value = "+$(this).html());
          console.log("value", value);
          if (value) {
            console.log("value set");alert("value set");
          } else {
            console.log("value not set");alert("value not set");
          }
        }
  });
});

你的条件是什么?根据我的想法,Var值将始终是真的。它不应该像Var值=这个。val==一些东西吗?真:假我同意任何值,如果有任何值,我需要在css中突出显示该字段。没有效果。。它仍然没有值集。@RahulI已经编辑了你的JSBIN演示。我已经编辑了我的答案。如果您还有任何问题,请告诉我。