请问用javascript如何实现警告框功能?
当下拉框显示“-------选择-----”(即SelectedValue=0),点击提交按钮,会弹出警告框,提示必须选择一个选项,请问用javascript如何实现这个功能?
后台代码:
private void Page_Load(object sender, System.EventArgs e){
CIPACE.BLL.PersonalInfo personinfo = new CIPACE.BLL.PersonalInfo();
DataTable dt = personinfo.GetEducation();
if (dt != null)
{
DataView dv = dt.DefaultView;
dv.Sort = "EducationID asc ";
this.ddlEducation.DataTextField = "EducationRank ";
this.ddlEducation.DataValueField = "EducationID ";
this.ddlEducation.DataSource = dv;
this.ddlEducation.DataBind();
ListItem DefaultItem = new ListItem( "-------选择------- ", "0 ");
this.ddlEducation.Items.Insert(0,DefaultItem);
}
}
前台代码:
<asp:dropdownlist id= "ddlPosition " runat= "server " Width= "120px "> </asp:dropdownlist>
显示内容:
<option value= "0 "> -------选择----- </option>
<option value= "1 "> 小学 </option>
<option value= "2 "> 中学 </option>
<option value= "3 "> 大专 </option>
<option value= "4 "> 本科 </option>
<option value= "5 "> 硕士 </option>
<option value= "6 "> 博士 </option>
[解决办法]
this.Button1.Attributes.Add( "onclick ", "return GoCheck(); ");function GoCheck(){ if(document.getElementById( "ddlPosition ").value == "0 ") { alert( "aaaaaaa "); return false;}}
[解决办法]
if(ddlPosition.SelectValue==0)
{
Response.Write( " <script> alert( 'Need choose one at least ! ') </script> ");
}
[解决办法]
在Page_Load里的最后加上:
Button.Attributes[ "onclick "] = string.Format( "javascript:if (document.getElementById( '{0} ').selectedIndex <= 0) { alert( '请选择 '); return false; } ", ddlPosition.ClientID);