[英]Javascript/HTML error “the value of the property “selectReason” is null or undefined, not a function object
Hi I am new to javascript and html and was wondering if someone could help me understand why this error keeps occuring. 嗨,我是javascript和html的新手,想知道是否有人可以帮助我理解为什么此错误不断发生。 The basic idea is that i have a dropdownlist that is populated with several options. 基本思想是我有一个下拉列表,其中填充了多个选项。 When the customer selects an option an onchange event fires that will write the value of the selected option into a textarea. 当客户选择一个选项时,将触发一个onchange事件,该事件会将所选选项的值写入文本区域。 However when i debug it I get the message above I have included the relevant snippets of code below: 但是,当我调试它时,我得到上面的消息,我在下面包含了相关的代码片段:
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>
You might be missing your <table>
tags as that was causing a problem for me. 您可能缺少<table>
标记,因为这对我造成了问题。 Your provided snippet also has var selecttext
and document.getElementById("Txt").value = selecttxt;
您提供的代码段还包含var selecttext
和document.getElementById("Txt").value = selecttxt;
<- missing an e. <-缺少e。
The provided snippet below works for me. 以下提供的代码段对我有用。 Possibly just failing to close a tag somewhere in your function/html. 可能只是无法在您的function / html中的某个位置关闭标签。
Also (this could be you added inline to provide a better picture) you should avoid inline styles and (to me) functions. 另外(这可能是您添加内联以提供更好的图片),您应该避免使用内联样式和(对我而言)函数。 Include a .css and .js file. 包括一个.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>
Maybe this could help you 也许这可以帮助您
<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>
hope :) 希望:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.