javascript报告“UNDEFINED”对于我分配给onchange事件的每个函数。代码有什么问题? [英] javascript reports "UNDEFINED" for every function I assign to the onchange event. what is wrong with the code?
本文介绍了javascript报告“UNDEFINED”对于我分配给onchange事件的每个函数。代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<head>
<script type="text/javascript">
function wtf()
{
window.alert("wtf???");
}</script>
</head>
<body onload="wtf()><!--{exception} 'wtf' is undefined-->
Kinds:<select id="Select1" name="D1" onchange="wtf();">
<option value='1'>1,Platform</option>
<option value='1'>1,yadda</option>
</select>Domains
推荐答案
试试这个:
Try this:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function wtf()
{
var select = document.getElementById("Select1");
// get the selected value
alert(select.value);
var selectedText = select.options[select.selectedIndex].text;
// get the selected text
alert(selectedText);
}</script>
</head>
<body>
Kinds: <select id="Select1" name="D1" onchange="wtf();">
<option value='1'>Platform</option>
<option value='2'>yadda</option>
</select> Domains
</body>
</html>
那里只需在select
元素中使用一个选项
即可更改;应该至少两个。
但它适用于我;有没有未定义。您可以编写alert
而不是window.alert
。此方法存在于窗口
对象中,因此您可以使用它。
-SA
There is nothing to change with just oneoption
in theselect
element; should be at least two.
But it works for me; there is nothing undefined. You can writealert
instead ofwindow.alert
. This method present in thewindow
object, so you can use it.
—SA
所以我是唯一一个不断收到错误的人说'wtf'函数(以及我编写的所有其他函数)都是未定义的?
So I am the only one that keeps getting the error saying that the function 'wtf'(and every other function I write) is undefined?
这篇关于javascript报告“UNDEFINED”对于我分配给onchange事件的每个函数。代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文