我的功能。当用户在select:
中更改项目时,我会选择索引$(function itemchange() {
var index = document.getElementById('columnList').selectedIndex;
if (index != -1) {
alert(index);
}
});
我的HTML标签。我把选择项目放在Panel中。该面板放在模态窗口中:
<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>
答案 0 :(得分:1)
你不应该这样做:
$(function itemchange() {
var index = document.getElementById('columnList').selectedIndex;
if (index != -1) {
alert(index);
}
});
因为现在函数的范围不在window对象中。 你应该这样做:
$(function(){
itemchange();
});
function itemchange() {
var index = document.getElementById('columnList').selectedIndex;
if (index != -1) {
alert(index);
}
}
然后函数的范围更改为window对象,然后在运行itemchange()
时执行。它也会在DOM加载时运行(我相信你想要的)。