简体   繁体   English

如何使需要动态创建的单选按钮-AngularJs

[英]How do I make dynamically created radio buttons required - AngularJs

I have a table with varying amount of rows. 我有一个表,其中行的数量不同。

<table>
<thead>
    <tr>
        <th class="text-center">Name</th>
        <th class="text-center">Select Supervisor</th>
    </tr>
</thead>
    <tbody>
        <tr ng-repeat="employeeInfo in employees" class="ng-cloak text-center">
            <td>{{employeeInfo.name}} {{employeeInfo.lastname}}</td>
            <td>
                <label><input type="radio" ng-model="employeeInfo.attributes[0].value" ng-value="true" ng-change="updateEmployeeAttribute(employeeInfo)">Approve</label>
                <label><input type="radio" ng-model="employeeInfo.attributes[0].value" ng-value="false" ng-change="updateEmployeeAttribute(employeeInfo)">Reject</label>
            </td>
        </tr>
    </tbody>
</table>
<div>
    <a id="Approve" class="btn btn-primary" ng-click="approveSupervisorChanges()">Submit</a>
</div>

One of the columns contain radio buttons "Approve" & "Reject" 列之一包含单选按钮“批准”和“拒绝”

How do I make it a requirement that in all rows the user has to have selected a radio button before he can submit ? 我如何要求用户在所有行中都必须选择一个单选按钮才能提交?

I tried this solution Validate Radio Button AngularJS by adding 我尝试通过添加以下方法来验证单选按钮AngularJS

ng-required="!employeeInfo.attributes[0].value"

But then it seems that accepted is a requirement, and if rejected is selected I can't submit 但是,似乎接受是一个要求,如果选择了拒绝,我将无法提交

you can put required attribute simply like: 您可以像下面这样放置必填属性:

<label><input type="radio" required ng-model="employeeInfo.attributes[0].value" ng-value="true" ng-change="updateEmployeeAttribute(employeeInfo)">Approve</label>
            <label><input type="radio" required ng-model="employeeInfo.attributes[0].value" ng-value="false" ng-change="updateEmployeeAttribute(employeeInfo)">Reject</label>

Ok so I got it working using by using ng-required like this. 好的,这样我就可以通过使用ng-required来使用它了。

<label><input type="radio" ng-required="employeeInfo.attributes[0].value" ng-model="employeeInfo.attributes[0].value" ng-value="true" ng-change="updateEmployeeAttribute(employeeInfo)">Approve</label>
<label><input type="radio" ng-required="!employeeInfo.attributes[0].value" ng-model="employeeInfo.attributes[0].value" ng-value="false" ng-change="updateEmployeeAttribute(employeeInfo)">Reject</label>

Which isn't really perfect(but it works) as when noting is selected, a popup highlights only the reject radio button and says "Please select one of these options" 这并不是真正的完美选择(但它可以工作),因为在选择“注意”时,弹出窗口仅突出显示“拒绝”单选按钮并显示“请选择以下选项之一”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AngularJS正式表格-如何使单选按钮成为必需 - AngularJS Formly Forms - How to Make Radio Buttons Required 如何将 eventListeners 附加到动态生成的单选按钮? - How do I attach eventListeners to dynamically generated radio buttons? 如何在AngularJS中使用单选按钮创建自定义过滤器 - How to make a custom filter with radio buttons in AngularJS 如何让不同的单选按钮做不同的事情? - How do I make different radio buttons do different things? 如何使用单选按钮为摄氏或华氏做出选项 - How do i make an option for Celsius or Fahrenheit with radio buttons 如何使动态创建的字段/元素成为必需? - How to make a dynamically created field/element required? 如何访问angularjs中动态创建的字段? - How do I access dynamically created fields in angularjs? 如何为每个动态创建的单选按钮放置错误消息 - How to put error messages for each dynamically created radio buttons 如何在MVC 4中将onclick事件添加到动态创建的单选按钮? - How to add onclick event to dynamically created radio buttons in MVC 4? 如何从JavaScript中动态创建的复选框和单选按钮获取输入? - How to get input from dynamically created checkboxes and radio buttons in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM