我遇到了一些问题,试图找到阻止表单提交的最佳行动方案,如果回答是/否问题或至少正确地给出错误消息。
首次尝试只需添加required =" true"到"否"按钮:
<th valign="top" align="left">
<label for="yesOrNo">
<strong>Are you a bad guy?</strong>
<br />
<br />
</label>
</th>
<td valign="top">
<label>
<input type="radio" name="yesOrNo" id="yesOrNo" value="yes" />Yes</label>
<br />
<label>
<input type="radio" name="yesOrNo" id="yesOrNo" required="true" value="no" />No</label>
<br />
<br />
</td>
</tr>
然后在那之后没有完全使用&#34;是&#34;仍然有效提交我尝试制作一个隐藏的领域,通过角度需要过去对我有用。当然这适用于文本字段和数字,我估计了在是 - 否单选按钮的情况下使用的内容:
<th valign="top" align="left">
<div ng-if="angularYesOrNo=true">You are uneligible for this promotion</div>
<label for="yesOrNo"><strong>Are you a bad guy?</strong>
<br />
<br />
</label>
</th>
<td valign="top">
<label>
<input type="radio" name="yesOrNo" ng-model="angularYesOrNo" id="yesOrNo" value="yes" />Yes</label>
<br />
<label>
<input type="radio" name="yesOrNo" id="yesOrNo" value="no" />No</label>
<input type="hidden" name="badGuyFlag" id="badGuyFlag" ng-required='angularYesOrNo=true' />
<br />
<br />
</td>
</tr>
我需要更改任何代码中的任何内容才能使其工作吗?或者是否需要在单独的脚本标记中添加任何函数?这整个下午一直绞尽脑汁。
答案 0 :(得分:0)
<强> Working Plunker 强>
看起来您需要在Angular Javascript文件中定义您的变量,如下所示:
<强>的Javascript 强>
$scope.angularYesOrNo = {bad:undefined};
您的ng-if语句需要两个相等的符号来评估您的angularYesOrNo
变量。
<强> HTML 强>
<div valign="top" align="left">
<div ng-if="angularYesOrNo.bad=='Yes' ">You are uneligible for this promotion</div>
<label for="yesOrNo"><strong>Are you a bad guy?</strong>
<br />
<br />
</label>
</div>
<div valign="top">
<label>
<input type="radio" name="yesOrNo" ng-model="angularYesOrNo.bad" id="yesOrNo" value="Yes" />
Yes
</label>
<br />
<label>
<input type="radio" name="yesOrNo" ng-model="angularYesOrNo.bad" id="yesOrNo" value="No" />
No
</label>
<br />
<br />
</div>
答案 1 :(得分:0)
您可以在表单上使用ng-submit
来设置提交处理程序,以便在提交表单数据之前对其进行验证。
<form name="myForm" novalidate ng-submit="processSubmit()">
<label><input type="radio" name="yesOrNo" ng-model="angularYesOrNo" id="yesOrNo" value="yes" /> Yes</label><br />
<label><input type="radio" name="yesOrNo" ng-model="angularYesOrNo" id="yesOrNo" value="no" /> No</label><br />
在你的控制器中定义功能。
$scope.angularYesOrNo = '';
$scope.processSubmit = function() {
if ($scope.angularYesOrNo == '' || $scope.angularYesOrNo == 'Yes') {
// the form should not be submitted
} else {
// process the form (submit or ajax)
}
};
如果您想从控制器触发表单提交,请参阅https://stackoverflow.com/a/25102791/5509627