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>
'' –
HI JB。謝謝。這是工作。 –