Textarea返回值始终为null

时间:2017-07-26 09:09:18

标签: javascript textarea

我正在尝试将用户从textarea输入到我的javascript中



function validateMessage() {
  var message = document.getElementById("message").value;
  window.alert(message);
}

<label>Message:</label>
<textarea rows="5" cols="50" id="message">Foo</textarea><button onclick="validateMessage()">Validate message</button>
&#13;
&#13;
&#13;

浏览器控制台一直给我这个消息

  

TypeError:document.getElementById(...)为null [了解详情]   contact.js:51:14

第51行是我的JavaScript中的实际行。 我已经阅读过大多数人都说使用.value的解决方案 但是你可以看到它返回null。 我无法弄清楚原因。

1 个答案:

答案 0 :(得分:1)

错误不是textarea返回值为null,而是message为空,这意味着没有标识为$i=0; foreach ($user_farms as $user_farm){ $user_farm_id = $user_farm->id; $user_flocks = \App\Flock::where('farm_id', '=', $user_farm_id)->get(); $no_flocks = \App\Flock::where('farm_id', '=', $user_farm_id)->get()->count(); $j=0; foreach ($user_flocks as $user_flock){ $user_flock_id = $user_flock->id; $weights = \App\Weight::where('flock_id', '=', $user_flock_id)->orderBy('week_id', 'asc')->get(); $weights_first = $weights ->first(); $start_week = $weights_first->week_id; foreach($weights as $weight ){ $age = $weight->week_id - $start_week ; $actual[$age] = $weight->female; } $graph = Lava::DataTable(); $graph->addStringColumn('Weeks') ->addNumberColumn('Standard') ->addNumberColumn('Actual') ->addRow(['1', $standard[1], ($actual[1]>0? $actual[1] : null) ]) ->addRow(['2', $standard[2], $actual[2]]) ->addRow(['3', $standard[3], $actual[3]]) ->addRow(['4', $standard[4], $actual[4]]) ->addRow(['5', $standard[5], $actual[5]]) ->addRow(['6', $standard[6], $actual[6]]) ->addRow(['7', $standard[7], $actual[7]]) ->addRow(['8', $standard[8], $actual[8]]) ->addRow(['9', $standard[9], $actual[9]]) ->addRow(['10', $standard[10], $actual[10]]); Lava::LineChart($i.'graph'.$j, $graph, [ 'title' => 'Weights of '. $user_flock->name ]); $j++; } $i++; } 的元素。

作为提示,请了解错误。仔细阅读它们,了解每个单词。