AngularJS radio button group with “other” option that includes a text field

后端 未结 2 769
萌比男神i
萌比男神i 2021-01-25 05:29

I have a form with three radio buttons in a group. The third radio button is \"Other\" and has a text field where the user can enter something in. I can make the radio button r

2条回答
  •  失恋的感觉
    2021-01-25 05:51

    Here's what I ended up with. It's a slight variation on hugo's answer. The main difference being that the "other" text stays visible on the screen. I just thought I'd document this as an alternative approach.

    Program:

    And then this $watch function in my controller:

    $scope.$watch('form.Program', function (mVal) {
        if (angular.isUndefined($scope.form)) return;
    
        if (mVal === 'Other') {
            $scope.form.OtherProgram = $scope.OtherProgram;
        } else {
            $scope.form.OtherProgram = null;
        }
    });
    

提交回复
热议问题