Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Angular验证选择字段和单选按钮_Javascript_Angularjs - Fatal编程技术网

Javascript 使用Angular验证选择字段和单选按钮

Javascript 使用Angular验证选择字段和单选按钮,javascript,angularjs,Javascript,Angularjs,我有一个表单,询问用户合同何时结束,他们可以输入一个月或一年,或者使用单选按钮选择“不确定”。我在2个选择字段中输入了必填项,而在单选按钮上没有输入。我还有一个功能,可以在单击单选按钮时将选择字段的值更改为“” 我遇到的问题是,如果用户单击单选按钮,选择将不再有效,因为它们是必需的。如果他们选择单选按钮,是否仍然可以使表单有效 以下是我所指表格的一部分: <form name="residentialForm" id="residentialForm" novalidate="true"

我有一个表单,询问用户合同何时结束,他们可以输入一个月或一年,或者使用单选按钮选择“不确定”。我在2个选择字段中输入了必填项,而在单选按钮上没有输入。我还有一个功能,可以在单击单选按钮时将选择字段的值更改为“

我遇到的问题是,如果用户单击单选按钮,选择将不再有效,因为它们是必需的。如果他们选择单选按钮,是否仍然可以使表单有效

以下是我所指表格的一部分:

<form name="residentialForm" id="residentialForm" novalidate="true" ng-submit="goToOffers()">
    <div class="question">
        <div class="questionBox">
            If so, when does the contract end?
        </div>
        <div class="inputGroup">
            Month <select ng-required="true" ng-model="form.contract_month" ng-change="contractSure()">
                <option value="">Select</option>
                <option ng-repeat="month in months" value="{{month.value}}">{{month.name}}</option>
            </select>
            Year <select ng-required="true" ng-model="form.contract_year" ng-change="contractSure()">
                <option value="">Select</option>
                <option ng-repeat="year in years" value="{{year}}">{{year}}</option>
            </select>
            <label><input type="radio" name="contract_end_notsure" value="notsure" ng-click="contractNotSure()" ng-model="form.contract_end"/><span>Not Sure</span></label>
        </div>
    </div>
    <input type="submit" id="goToOffers" class="appSubmit" value="Submit"/>
    <pre>{{form | json}}</pre>
</form>

如果是,合同什么时候结束?
月
挑选
{{month.name}
年
挑选
{{year}
不确定
{{form | json}}
不确定是否需要,但下面是javascript:

<div class="inputGroup">
    Month
    <select ng-required="form.contract_end!=notsure" ng-model="form.contract_month" ng-change="contractSure()">
        <option value="">Select</option>
        <option ng-repeat="month in months" value="{{month.value}}">{{month.name}}</option>
    </select>
    Year
    <select ng-required="form.contract_end!=notsure" ng-model="form.contract_year" ng-change="contractSure()">
        <option value="">Select</option>
        <option ng-repeat="year in years" value="{{year}}">{{year}}</option>
    </select>
    <label>
        <input type="radio" name="contract_end_notsure" value="notsure" ng-click="contractNotSure()" ng-model="form.contract_end" /><span>Not Sure</span>
    </label>
</div>
app.controller('ResidentialCtrl',['$scope','$rootScope',函数($scope,$rootScope){
$scope.years=[];
$scope.months=[
{值:'01',名称:'Janary'},
{值:'02',名称:'二月'},
{值:'03',名称:'March'},
{value:'04',name:'April'},
{值:'05',名称:'May'},
{值:'06',名称:'June'},
{值:'07',名称:'July'},
{值:'08',名称:'August'},
{值:'09',名称:'九月'},
{值:'10',名称:'10'},
{值:'11',名称:'11'},
{值:'12',名称:'December'}
];
$scope.form={};
$scope.zipcode=$rootScope.zipcode;
var currentYear=新日期().getFullYear();

对于(var i=0;i您可以通过在
ng required=“expression”

在您的情况下,
ng required
指令如下所示

HTML


月
挑选
{{month.name}
年
挑选
{{year}
不确定
希望这能帮助你。谢谢

<div class="inputGroup">
    Month
    <select ng-required="form.contract_end!=notsure" ng-model="form.contract_month" ng-change="contractSure()">
        <option value="">Select</option>
        <option ng-repeat="month in months" value="{{month.value}}">{{month.name}}</option>
    </select>
    Year
    <select ng-required="form.contract_end!=notsure" ng-model="form.contract_year" ng-change="contractSure()">
        <option value="">Select</option>
        <option ng-repeat="year in years" value="{{year}}">{{year}}</option>
    </select>
    <label>
        <input type="radio" name="contract_end_notsure" value="notsure" ng-click="contractNotSure()" ng-model="form.contract_end" /><span>Not Sure</span>
    </label>
</div>