Javascript/HTML错误“属性”selectReason“的值为null或未定义,而不是函数对象
Javascript/HTML error “the value of the property “selectReason” is null or undefined, not a function object
嗨,我是javascript和html的新手,想知道是否有人能帮助我理解为什么这个错误不断发生。基本的想法是,我有一个下拉列表,其中填充了几个选项。当客户选择一个选项时,会触发onchange事件,将所选选项的值写入文本区域。然而,当我调试它时,我收到了上面的消息,我在下面包含了相关的代码片段:
function selectReason() {
var selectindex = document.getElementById("selectmessage").selectedIndex;
var selecttext = document.getElementById("selectmessage").value;
document.getElementById("Txt").value = selecttxt;
}
<TR style="DISPLAY: none; VISIBILITY: hidden" id=sel>
<TD width="60%" align=left>Please select a reason</TD>
<TD width="40%" align=left>
<SELECT id="selectmessage" onchange="selectReason()" style="WIDTH: 425px"></SELECT>
</TD>
</TR>
您可能缺少<table>
标记,因为这给我带来了问题。您提供的代码段也有var selecttext
和document.getElementById("Txt").value = selecttxt;
<-缺少一个e.
下面提供的代码片段对我很有用。可能只是未能关闭函数/html中的某个标记。
此外(这可能是你添加内联以提供更好的图片),你应该避免内联样式和(对我来说)函数。包括一个.css和.js文件。
function selectReason() {
var selectindex = document.getElementById("selectmessage").selectedIndex;
var selecttext = document.getElementById("selectmessage").value;
var elem = document.getElementById("hey");
elem.innerHTML = selecttext;
}
// Events to attach
function init () {
var elem = document.getElementById('selectmessage');
elem.addEventListener('change', selectReason)
}
// Run 'init' when the DOM is ready (jQuery's $(document).ready() )
document.addEventListener("DOMContentLoaded", init, false);
<table>
<TR>
<TD>Please select a reason</TD>
<TD>
<SELECT id="selectmessage">
<option>Hello</option>
<option>Yes</option>
</SELECT>
</TD>
</TR>
<tr>
<td id='hey'></td>
</tr>
</table>
也许这可以帮助您
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<table>
<tbody>
<tr>
<td>
<select name="" id="selectmessage">
<option value="red">red</option>
<option value="gree">green</option>
<option value="blue">blue</option>
</select>
</td>
</tr>
</tbody>
</table>
<textarea name="" id="txt" cols="30" rows="10">
</textarea>
<script type="text/javascript">
$(document).on('ready', function(){
$('#selectmessage').on('change', function(){
var val = $(this).val();
var txt = $('#txt');
txt.val(val);
});
});
</script>
</body>
</html>
希望:)
相关文章:
- 无法获取属性'selectedIndex'的未定义引用或null引用
- 为什么localStorage[“..”]未定义,而localStorage.getItem(“..”)为null
- 当null应该在javascript中使用而不是未定义时
- React.createElement:类型不应为null或未定义
- 对象为null或IE9中存在未定义错误
- 如何在JavaScript中检查变量是否为null和/或未定义
- Microsoft JScript运行时错误:无法获取属性'的值;样式':对象为null或未定义
- 无法获取属性'字符串'的未定义或null引用IE 11 ASP.NET
- SCRIPT5007:无法获取属性'长度'的未定义引用或null引用
- 无法获取属性'setActionableButtonState'的未定义引用或null引用
- Highcharts无法设置属性'的值;要点':对象为null或未定义
- IE错误:无法设置属性'的值;样式':对象为null或未定义
- moment:Array.prototype.some在null或未定义时被调用
- 无法获取属性'偏移'的未定义引用或null引用
- 具有null或未定义值的JavaScript字符串串联行为
- Javascript-函数获取数组元素参数未定义/为null
- 无法在jasmine it函数中将未定义或null转换为对象
- JS:未定义值和未定义类型,以及null值和null类型之间的区别
- IE:无法获取属性'的值;clientWidth':对象为null或未定义
- 比较未定义/null/NaN与ActionScript中的number