我正在使用select2插件。我想在用户忘记选择提交时的下拉值时向用户显示错误。但是发生了什么意味着它正确地显示了错误消息,但是在选择了下拉值后显示错误消息。
我的代码是,
<select name="books" ng-model="books" wb-select2 initial-select-key="authour" target-id="books" ng-options="book.authour for book in books track by book.id" class="select2" required="true">
答案 0 :(得分:0)
您可以将其包装在表单标记中,并在其上使用表单验证,如下所示:
<强> HTML:强>
<form name="formName" ng-submit="formName.$valid&&accountSave()" novalidate>
<select name="books" ng-model="books" wb-select2 initial-select-key="authour" target-id="books" ng-options="book.authour for book in books track by book.id" class="select2" required="true">
<p ng-if="formName.$submitted&&formName.books.$error.required" class="error">Books is required</p>
</form>
<强> JS:强>
$scope.accountSave=function(){
//post form details to server
}