我想使用 vb 限制从 asp.net 中的列表框中选择的项目数。我正在尝试使用javascript。但是,不工作。需要帮忙
<asp:ListBox ID="lbprefferedlocation" runat="server" DataSourceID="locations"
DataTextField="City_Name" OnSelectedIndexChanged="chkCount(this)" DataValueField="City_Name" SelectionMode="Multiple"></asp:ListBox>
<asp:HiddenField ID="hiddenChkCount" runat="server" />
<asp:SqlDataSource ID="locations" runat="server"
ConnectionString="<%$ ConnectionStrings:JPConnString %>"
SelectCommand="SELECT [City_Name] FROM [State_Info]"></asp:SqlDataSource>
function chkCount(obj)
{
if(obj.checked==true)
{
if( document.getElementById( '<%=hiddenChkCount.ClientID %>' ).value >= 5 )
{
alert('You cannot select more than 5 items.');
obj.checked=false;
}
else
{
document.getElementById( '<%=hiddenChkCount.ClientID %>' ).value = parseInt( document.getElementById( '<%=hiddenChkCount.ClientID %>' ).value ) + 1;
}
}
else
{
document.getElementById( '<%=hiddenChkCount.ClientID %>' ).value = parseInt( document.getElementById( '<%=hiddenChkCount.ClientID %>' ).value) - 1;
}
}