简体   繁体   中英

Get the RadioButton Value on click using jquery and disabling, enabling text boxes accordingly, Asp.net mvc

I have two radio button

input type = radio name = "Level" value = "P"

input type = radio name = "Level" value = "S"

I have two textboxes:

input type = "text" name = "inpu11" disabled = "disabled"

input type = "text" name = "inpu12" disabled = "disabled"

My question is,

How Can I identify to which button I've selected ? How can I retrieve the selected radio Id ? I need to enable the first textbox if the selected value is "P" and disabled if 'S'

You asked 3 questions. I'll answer the third question first. You can do this with Javascript/JQuery fairly easily.

View.aspx:

<form action='/MyController/MyAction' method='post'>
    <div>
        <input type='radio' name='selectRadios' value='P' />
        <input type='text' name='firstTextBox' value='P' />
    </div>

    <div>
        <input type='radio' name='selectRadios' value='S' />
        <input type='text' name='secondTextBox' value='S' />
    </div>

    <input type='submit' value='Submit Values to ActionMethod' />
</form>

<script type='text/javascript'>
    $(document).ready(function() {
        $("input[name=selectRadios]").click(function() {
            switch($(this).val()) {
                case 'S':
                    $("input[name=firstTextBox]").attr("disabled", "disabled");    
                    break;
                case 'P':
                    $("input[name=firstTextBox]").attr("disabled", "");    
                    break;
            }
        });
    });
</script>

I think your first and second questions will get answered pretty easily by MVC's modelbinder.

MyController.cs (action method only):

//argument names match form element names from view.aspx
public ActionResult MyAction(string selectRadios, string firstTextBox, string secondTextBox)
{
    //do something with your form values
}

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