由于业务需求,做了一个动态切换select的功能。
option是必选的,所以加了required,但是每次提交的时候会出现
An invalid form control with name='' is not focusable.
这种错误。
以下是代码。当点击 子网时,显示select1 当点击 专题时,显示select2
<form ng-submit="news_submit()" class="pure-form" >
<fieldset style="margin: auto;
width: 80%;">
<p class="pure-control-group">
<label for="maintitle">主标题</label>
<input id="maintitle" type="text" placeholder="主标题" required ng-model="news_edit.maintitle" required/>
</p>
<p class="pure-control-group">
<label for="subhead">副标题</label>
<input id="subhead" type="text" placeholder="副标题" ng-model="news_edit.subhead"/>
</p>
<p class="pure-control-group">
<label for="password">视频地址</label>
<input id="password" type="text" placeholder="视频地址" ng-model="news_edit.videourl"/>
</p>
<p class="pure-control-group">
<label for="email">题图</label>
<input id="email" type="file" class="pure-button pure-button-primary" ng-model="news_edit.titlepic"/>
</p>
<a class="pure-control-group">
<label for="state">栏目
</label>
<p style="margin:10px 0 10px 0; display: inline-block;">
<a ng-click="change_selecter(1)" class="pure-button "
ng-class="select_range?'pure-button-primary':''">子网</a>
<a ng-click="change_selecter(0)" class="pure-button "
ng-class="select_range?'':'pure-button-primary'">专题</a>
</p>
<p ng-include="select_range?'selecter2':'selecter1'"></p>
</a>
</fieldset>
<p class="pure-controls">
<p id="neditor" style="min-height:500px;" ng-model="news_edit.content" contenteditable="true"
place-holder="输入正文">
</p>
<button type="submit" class="pure-button pure-button-primary">Submit</button>
</p>
</form>
<button id="btn1">获取内容</button>
</p>
<script id="selecter1" type="text/ng-template">
<p>
<select ng-model="col_selectlv[0]" ng-options="x.colname for x in col_select[0]" required>
<option></option>
<option></option>
</select>
<select ng-model="col_selectlv[1]" ng-options="x.colname for x in col_selectlv[0].children"
ng-show="col_selectlv[0].children"
required>
<option></option>
</select>
<select ng-model="col_selectlv[2]" ng-options="x.colname for x in col_selectlv[1].children"
ng-show="col_selectlv[1].children" required>
<option></option>
</select>
<select ng-model="col_selectlv[3]" ng-options="x.colname for x in col_selectlv[2].children"
ng-show="col_selectlv[2].children" required>
<option></option>
</select>
<select ng-model="col_selectlv[4]" ng-options="x.colname for x in col_selectlv[3].children"
ng-show="col_selectlv[3].children" required>
<option></option>
</select>
<select ng-model="col_selectlv[5]" ng-options="x.colname for x in col_selectlv[4].children"
ng-show="col_selectlv[4].children" required>
<option></option>
</select>
</p>
</script>
<script id="selecter2" type="text/ng-template">
<p>
<select ng-model="col_selectlv[0]" ng-options="x.colname for x in col_select[1]" required>
<option></option>
<option></option>
</select>
<select ng-model="col_selectlv[1]" ng-options="x.colname for x in col_selectlv[0].children"
ng-show="col_selectlv[0].children"
required>
<option></option>
</select>
<select ng-model="col_selectlv[2]" ng-options="x.colname for x in col_selectlv[1].children"
ng-show="col_selectlv[1].children" required>
<option></option>
</select>
<select ng-model="col_selectlv[3]" ng-options="x.colname for x in col_selectlv[2].children"
ng-show="col_selectlv[2].children" required>
<option></option>
</select>
<select ng-model="col_selectlv[4]" ng-options="x.colname for x in col_selectlv[3].children"
ng-show="col_selectlv[3].children" required>
<option></option>
</select>
<select ng-model="col_selectlv[5]" ng-options="x.colname for x in col_selectlv[4].children"
ng-show="col_selectlv[4].children" required>
<option></option>
</select>
</p>
</script>
由于这是一个新闻发布编辑器,需要选择最终子栏目,所以预设了6个层级,当n层级有子栏目时候,显示n+1的select。
搜索了一下,建议的解决方案是先删掉所有的required,然后在提交的时候验证,想知道这是不是唯一的方法?
已解決。
出現該錯誤的原因是chrome發現了有隱藏(display:none)的required需求元素,所以會出錯。
將ng-show改為ng-if,從隱藏標籤變成移除dom,可以避免這個錯誤。