user1187282
user1187282

Reputation: 1237

How to get radio button value as a number

Is there a way to get the value of the radio button as an integer? I'm submitting a form that but the value that I'm getting from radio buttons is always a string, I've tried many solutions but none of them work.

  <div class="form-check form-check-inline">
          <input class="form-check-input" formControlName="note" type="radio"  value='3' >
          <label class="form-check-label" for="inlineRadio3">Advanced</label>
        </div>

 <div class="form-check form-check-inline">
          <input class="form-check-input" formControlName="note" type="radio"  value=3 >
          <label class="form-check-label" for="inlineRadio3">Advanced</label>
        </div>

here is what i get when i submit the form :

enter image description here

in order to the web API works, I need to have like this note : 3, please help. [UPDATE]

here's my how i'm submitting the form:

this.Myser.AddAll(this.rateform.value.lstrates).subscribe(data => console.log(data) );

and this is how i declared my form :

this.rateform = this.fb.group({
      lstrates: this.fb.array([
        this.fb.group({
          fieldId: [''],
          memberId: [''],
          note: [''],
          fieldName:['']
        }),
      ]),
    });

but the problem is , the note attribut must be a number. so i dont know how to get it from the radio button as integer, should i create another list and convert it to integer ? or there is a better way ?

Upvotes: 8

Views: 9528

Answers (1)

satanTime
satanTime

Reputation: 13539

You need to use [value]="3" and skip value="3" in this case, it allows to pass any type as a value including objects etc.

See: angular doc binding syntax

Also you might import FormsModule if it wasn't.

Demo: https://stackblitz.com/edit/angular-ivy-ll3ckc?file=src%2Fapp%2Fapp.component.html

Upvotes: 14

Related Questions