简体   繁体   中英

How to get RadioButton`s label from html MVC

I`ve got three radio buttons in my form. Every rbutton has own label as below

 <form method="post">

        <input type="radio" name="radio"  "onclick="removeField()">
        <label>Text1</label>
             <br>
        <input type="radio" name="radio" onclick="removeField()">
        <label>Text2</label>
            <br />
        <input type="radio" name="radio"  onclick="addField()">
        <b>Another</b>
            <br />

How I can get in my c# code label of radiobutton which is on?

You could just make the radio button's value be the same text as the label. When you post the form it should give you a key/value pair of 'radio=Text1' (or whatever.

<form method="post">

        <input type="radio" name="radio" value="Text1" "onclick="removeField()">
        <label>Text1</label>
             <br>
        <input type="radio" name="radio" value="Text2" onclick="removeField()">
        <label>Text2</label>
            <br />
        <input type="radio" name="radio" value="Another"  onclick="addField()">
        <b>Another</b>
  ...

A few additional notes:

If label wraps the radio button, it becomes clickable and will toggle the radio button. Make sure to put the 'onclick' on the label tag if you do that.

The name 'radio' for a form field is not illegal, but I would suggest it's complicated. That radio button represents a property of your viewmodel (or of something) so I would suggest calling it something like 'SelectedText' or whatever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM