2017-05-25 61 views
0

我想添加一個下拉列表與「其他」選項。如果用戶選擇「其他」,其他(請指定)輸入框將變爲強制。我應該如何驗證這種情況?現在我添加了Javascirpt代碼來驗證這一點。無論如何要做到這一點就像「請指明其他原因」。Angular JS下拉列表選擇「其他」選項和其他輸入成爲強制

<form name="myForm"> 

    <div ng-app="myApp" ng-controller="myCtrl"> 

    Reason : <select ng-model="reason" ng-options="x for x in names" required></select> <span ng-show="myForm.reason.untouched">The reason is required.</span> 

<p>Others (specify): <input type="text" ng-model="others"></p> 


    </div> 
    </form> 




    <script> 
    var app = angular.module('myApp', []); 
    app.controller('myCtrl', function($scope) { 
     $scope.names = ["reason1", "reason2", "reason3","Others"]; 

$ scope.addItem =函數(){

 if ($scope.reason == "Others" && !$scope.others) 
    { 
     window.alert("Please specify others"); 
    } 

    if($scope.others && $scope.reason!='Others') 
    { 
     window.alert("Please select reason others"); 
    } 


    }); 
    </script> 
+0

'' –

+0

HI JB。謝謝。這是工作。 –

回答

1

角有一個NG-所需的屬性,使您可以有條件地設置它。

<form name="myForm"> 

<div ng-app="myApp" ng-controller="myCtrl"> 

Reason : <select ng-model="reason" ng-options="x for x in names" required> 
</select> <span ng-show="myForm.reason.untouched">The reason is required. 
</span> 

<p>Others (specify): <input type="text" ng-model="others" ng-required="reason == 'Others'"></p> 


</div> 
</form> 
+0

嗨鎳。我得到了它的工作。非常感謝。 :) –

相關問題