2016-03-08 80 views
0

我有一个asp dropdownlist <asp:DropDownList ID="ddIndProvince" data-style="btn-default" CssClass="form-control input" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddIndProvince_SelectedIndexChanged" TabIndex="8"></asp:DropDownList>页面加载事件我在索引0处插入了“--Select--”项目。 现在我遇到了问题,我正在使用引导验证器。它的返回值对“ - 选择 - ”项有效。像下面的图片。 enter image description here使用引导验证器验证ASP.NET Dropdownlist控件

如何this.Below我们我当前的脚本

<script type="text/javascript"> 
    $(document).ready(function() {   
      $('#form1').bootstrapValidator({ 
       container: '#messages', 
       feedbackIcons: { 
        valid: 'glyphicon glyphicon-ok', 
        invalid: 'glyphicon glyphicon-remove', 
        validating: 'glyphicon glyphicon-refresh' 
       }, 
       fields: {     
        <%=ddIndProvince.UniqueID%>:{ 
         validators:{ 
          notEmpty:{ 
           messages:'please select province' 
          } 
         } 
        }        
       } 
      }); 
     });   
    </script> 

回答

1

bootstrapValidator被验证<option>标签的value属性在你的HTML标记,我可以证实。

您的JavaScript代码是正确的。你需要做的就是确保什么,该值属性为空:

更改您的标记从:

<option value="Something is in here">--Select--</option> 

要:

<option value="">--Select--</option> 

为此在你的Page_Load方法:

protected void Page_Load(object sender, EventArgs e) 
{ 
    /*...*/ 

    //Insert new Item at Index 0 with text "--Select--" and no value 
    ddIndProvince.Items.Insert(0, new ListItem("--Select--", String.Empty)); 
    ddIndProvince.SelectedIndex = 0; 
} 
+0

很好用!谢了哥们! –