【问题标题】:AngularJS select required validationAngularJS 选择所需的验证
【发布时间】:2015-06-27 19:19:59
【问题描述】:

以下是我的代码 sn-p。我想使用 Angular 验证我的选择框。

<form name="myForm">
  <select name="category_1"  ng-model="catid" id="category_1" ng-options="category.catid for catid in categories" required>
    <option value="" selected=""></option>
    <option value="1">music</option>
    <option value="2">internet</option>
  </select>
  <button ng-disabled="!myForm.$valid" ng-click="uploadPic(picFile)">Submit</button>
</form>

但无法提交表单我在 chrome 中收到此错误,并且提交按钮不起作用

name='category_0' 的无效表单控件不可聚焦。

这里是plunker

【问题讨论】:

  • category.catid for catid in categories: 无效。 category 是什么?
  • 我怎样才能使它有效?
  • category.catid for category in categories 将是有效的,假设类别中的对象有一个名为 catid 的字段。为什么不阅读文档:docs.angularjs.org/api/ng/directive/ngOptions
  • 因为我的英文不够,我试过了,现在无法点击提交按钮

标签: angularjs select required


【解决方案1】:

问题不在于选择框,可能问题在于您有另一个选择框(名称为category_0)或其他输入字段(名称为category_0),它具有必填 属性,它被隐藏或不可见。

因此,当您尝试提交表单时,它会尝试将其聚焦,但这是不可能的。确保名称为 category_0 的元素不在 ng-hide 块内,或在具有 false 条件的 ng-show 内。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 2018-01-06
    相关资源
    最近更新 更多