2

将 HTML5“必需”属性用于无线电类型输入和 Angularjs 表单验证时,我得到了奇怪的结果

我的输入:

<form name="myForm">
    <div ng-repeat="i in [0,1,2]">
        <input type='radio' name='var1' ng-model='var1' value='1' required />1<br />
        <input type='radio' name='var1' ng-model='var1' value='2' required />2<br />
        <input type='radio' name='var1' ng-model='var1' value='3' required />3<br />
        <span ng-show="myForm.var1.$error.required" ng-click="">Choose an option.</span>
        <button ng-disable="myForm.$invalid">Submit</button>
    </div>
</form>

当我选择一个选项时,其他选项将被取消选择。此外,两者的表单验证都被激活/停用。

4

1 回答 1

1

是的,这是因为“名称”属性被重复了,试试这个:

<form name="myForm">
    <div ng-repeat="i in [0,1,2]">
        <input type='radio' ng-model='i.var1' value='1' required />1<br />
        <input type='radio' ng-model='i.var1' value='2' required />2<br />
        <input type='radio' ng-model='i.var1' value='3' required />3<br />
        <span ng-show="i.var1==''" ng-click="">Choose an option.</span>
        <button ng-disable="i.var1==''">Submit</button>
    </div>
</form>

删除名称并使用 i.var1=='' 直接执行表单验证/禁用

于 2013-10-04T17:22:33.677 回答