函数只被处理一次,然后浏览器给出错误“函数未定义”
functions are processed only once and then browser give error "function is not defined"
我的功能。当用户在select:
中更改项时,我得到选中的索引$(function itemchange() {
var index = document.getElementById('columnList').selectedIndex;
if (index != -1) {
alert(index);
}
});
我的html标签我在面板中选择项目。这个面板放置在模态窗口:
<asp:Panel ID="ModalPanel" runat="server" Width="700px" Height="500px" BackColor="GhostWhite">
<div>
<br />
<div style="float:left;">
<select size="4" id="columnList" onchange="itemchange" style="height:200px;">
<option value="Code">Code</option>
<option value="Name">Name</option>
<option value="P">P</option>
<option value="Desc">Desc</option>
<option value="Object">Object</option>
<option value="User name">User name</option>
<option value="Num">Num"</option>
<option value="Commant">Commant</option>
<option value="Prod">Prod</option>
<option value="Test">Test</option>
</select>
</div>
<div style="clear:left;">
<asp:Button ID="OKButton" runat="server" Text="Close" />
</div>
</div>
</asp:Panel>
你不应该这样做:
$(function itemchange() {
var index = document.getElementById('columnList').selectedIndex;
if (index != -1) {
alert(index);
}
});
因为现在函数的作用域不在窗口对象中。你应该这样做:
$(function(){
itemchange();
});
function itemchange() {
var index = document.getElementById('columnList').selectedIndex;
if (index != -1) {
alert(index);
}
}
然后函数的作用域更改为窗口对象,然后在运行itemchange()
时执行。它也会在DOM加载时运行(我相信你想要的)。
相关文章:
- 尝试调用AngularJS上的函数时出错
- 得到"未定义不是函数“;使用显示原型图案时出错
- 从函数中的函数返回数组时出错
- 从 java 脚本调用函数时 PHP 中出错
- 使用PHP和Javascript在函数内部传递alphaneumeric值作为参数时出错
- 比较$watch函数内的基元类型时出错
- CakePHP错误:在非对象上调用成员函数get(),使用cakejavascript助手时出错
- 构造函数出错
- javascript函数出错
- AngularJS:没有定义$rootscope的全局函数出错,没有定义$scope
- Java脚本函数不;一个函数出错后无法工作
- OnClick函数出错
- 函数b()出错
- for循环中的JavaScript匿名函数出错
- 使用带有href的attr函数出错
- 从外部文件调用JavaScript函数出错
- 使用本地存储的可排序列表函数出错
- 确定数字中位数的函数出错
- PHP邮件函数发送电子邮件,但AJAX函数出错
- ajax函数出错,尽管它转到了我的webservice