How to concatenate Radiobuttons values into Razor/HTML?

Asked

Viewed 274 times

0

I have a question and it’s kind of hard to find a practical way to solve.

I created a groupbox with 10 radiobuttons. I would like to know the best practice so that when the user marks one of them I store this value in a variable so that I can concatenate these values with 2 more groups.

Example:

RadioButton1 of groupbox1 worth 1, the RadioButton of groupbox2 vale 2.

He should take the numbers and put one in each variable so that I can concatenate side by side resulting in (12) and not (3), and after that do a power calculation using the value of the 3rd groupbox.

  • You want in Razor?

  • I’m a little young in c# and I don’t know Razor. I edited the question title. But the case is that I need to concatenate two values one of each raddiobutton and raise the power of a raddiobutton value of the ultima groupbox. However each raddiobutton must contain a value for example. Raddiobutton1 has to store the value 10; so I can’t do it with just true or false; I don’t know if I can express myself correctly.

  • This can be solved by [tag:javascript]. I edited your question to reach the appropriate audience.

  • Thank you very much! I’ll start formatting the text the same way you did. I’m sorry I’m inexperienced at stackoverflow.

  • @gypsy the question refers to c# same and not html and javascript.

  • Actually the focus of the question is not on [tag:c#]. Predominantly the calculation you need is a client behavior, solvable by [tag:javascript]. C# may end up coming in later, but not now.

  • I will try to explain what I am trying to do. It is a small program to calculate the value of the electronic component resistor. Each resistor has a different resistance value and its value is indicated by a color code as well as its tolerance. a groupbox1 represents the first colour range of the resistor to groupbox2 represents the second colour band and the groupbox3 represents your multiplier I created a form and inserted these components the case is I would like to know the best syntax to solve this I got lost because in php would be very easy for me to do.

  • So, as I told you and will repeat: the behavior of RadioGroups shall be calculated as a screen. To send the final result pro Controller (in the case of a MVC application), these values must first be calculated on screen.

  • It’s really just one form even could be something very simple and not a MVC application, but now I understood what you meant.

Show 4 more comments

1 answer

2


As @cigano said the solution of this problem may be better in Javascript.

Follow example using HTML + Javascript with Jquery and the JSFIDDLE working

HTML:

<form>
    <fieldset>
        <legend>Grupo 1 (Primeiro Dígito):</legend>
        <input type="radio" name="grupo1" value="1"/> 1<br/>
        <input type="radio" name="grupo1" value="2"/> 2<br/>
        <input type="radio" name="grupo1" value="3"/> 3<br/>
    </fieldset>
    <fieldset>
        <legend>Grupo 2 (Segundo dígito):</legend>
        <input type="radio" name="grupo2" value="1"/> 1<br/>
        <input type="radio" name="grupo2" value="2"/> 2<br/>
        <input type="radio" name="grupo2" value="3"/> 3<br/>
    </fieldset>
    <fieldset>
        <legend>Grupo 3 (Potencia):</legend>
        <input type="radio" name="grupo3" value="1"/> 2<br/>
        <input type="radio" name="grupo3" value="2"/> 3<br/>
        <input type="radio" name="grupo3" value="3"/> 3<br/>
    </fieldset>
    Resultado:
    <input type="text" id="txtResultado"/>
</form>

Javascript:

$(document).ready(function(){
    $('input:radio').on('click',CalculaResultado);
});

function CalculaResultado(oRadio){
    var grupo1 = $('input:radio[name="grupo1"]:checked').val();
    var grupo2 = $('input:radio[name="grupo2"]:checked').val();
    var grupo3 = $('input:radio[name="grupo3"]:checked').val();
    var resultado = parseInt(grupo1.toString() + grupo2.toString()) 
    resultado = Math.pow(resultado,parseInt(grupo3))
    if (!isNaN(resultado))
        $('#txtResultado').val(resultado);
    else 
        $('#txtResultado').val(0);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.