简体   繁体   English

Textarea值为NULL javascript

[英]Textarea value is NULL javascript

I want to display the text area value. 我想显示文本区域值。 I generate it with PHP code This code is in the looping 我用PHP代码生成它这个代码在循环中

 for($i=0;$i<$piecount;$i++)
    {
    echo "<tr><td class=\"forma\" align=\"center\">".$compc[$i]."</td>";
    if($i==0)
    {
       echo "<td align=\"center\" class=\"forma\" rowspan=".$pcg."  style=\"\"> 
      <textarea name=\"eva\" id=\"evatext\"   class=\"textEva\" >
      </textarea> 
      <input type=\"image\"src=\"image/save.jpg\"class=\"imgClass\" onclick=\"EvaCek();\" name=\"save\"></td></tr>";

} } }}

Also I have a html code for dummy my id for inputdate which is displayed none 另外我有一个用于输入日期的虚拟我的id的html代码,它没有显示

<input name="evadate" id="inputdate" style="display:none">

And I pass it to javascript 我把它传递给javascript

function EvaCek()
{
    var dateobj = new Date();
        var month = dateobj.getUTCMonth()+1;
        var day = dateobj.getUTCDate();
        var year = dateobj.getUTCFullYear();
        var tes=document.getElementById("inputdate").innerHTML= year+"-"+month+"-"+day;
        var bla=document.getElementById("evatext").value;

    if(bla!="")
    {
     alert(tes);
     alert(bla);

    }

} }

When I type something into the text area and save it, it does the alert, but only the date, the text area is NULL. 当我在文本区域中键入内容并保存它时,它会发出警报,但只有日期,文本区域为NULL。 What is wrong in here? 这里有什么问题? Thank you 谢谢

It is because ID should be unique for each input. 这是因为每个输入的ID都应该是唯一的。

IDs must be unique ID必须是唯一的

For more info, check here Unique ID 有关详细信息,请在此处查看唯一ID

So, access it through class name. 因此,通过类名访问它。 I'm not seeing anywhere 'inputdate' . 我没有看到任何'inputdate' So, i didn't edited it. 所以,我没有编辑它。 But, Don't access inputdate or textarea through ID . 但是,不要通过ID访问inputdatetextarea Because, forloop is creating numerous number of <textarea></textarea> and every textarea is having the same id . 因为, forloop正在创建大量的<textarea></textarea>并且每个textarea都具有相同的id Avoid accessing with ID . 避免使用ID访问

<?
for($i=0;$i<$piecount;$i++)
{
    echo "<tr><td class=\"forma\" align=\"center\">".$compc[$i]."</td>";
    if($i==0)
    {
        echo "<td align=\"center\" class=\"forma\" rowspan=".$pcg."  style=\"\"> 
            <textarea name=\"eva\" id=\"evatext\" class=\"textEva\" ></textarea> 
            <input type=\"image\"src=\"image/save.jpg\"class=\"imgClass\" onclick=\"EvaCek();\" name=\"save\">
        </td>
        </tr>";
    }
}
?>

<?
function EvaCek()
{
  var dateobj = new Date();
      var month = dateobj.getUTCMonth()+1;
      var day = dateobj.getUTCDate();
      var year = dateobj.getUTCFullYear();
            //change for inputdate. Access it through class name.
      var tes=document.getElementByClass("inputdate").innerHTML= year+"-"+month+"-"+day;
      var bla=document.getElementByClass("textEva").value;

  if(bla!="")
  {
   alert(tes);
   alert(bla);

  }
}
?>

use this to get value of text area ... demo 用它来获得文本区域的价值...... 演示

document.getElementById("evatext").innerHTML; 

full code 完整代码

function EvaCek()
{
    var dateobj = new Date();
    var month = dateobj.getUTCMonth()+1;
    var day = dateobj.getUTCDate();
    var year = dateobj.getUTCFullYear();
    var tes=document.getElementById("inputdate").innerHTML= year+"-"+month+"-"+day;
    var bla=document.getElementById("evatext").innerHTML;// use this

    if(bla!="")
    {
     alert(tes);
     alert(bla);

    }
}

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

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