Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if checkbox is checked in view page with razor and if checked display a textbox

How can I check if a checkbox is checked in view page with razor and if checked display a textbox!

I'm new in asp.net mvc and razor, still learning.

View Code

@Html.CheckBoxFor(m => m.SupportRequired)    
@Html.TextBoxFor(m => m.AssistName new { @class = "form-control" })
like image 862
Tom Avatar asked Jan 19 '26 10:01

Tom


1 Answers

You can use JavaScript like this:

@Html.CheckBoxFor(m => m.SupportRequired , new { id = "MyChk", onchange = "valueChanged()"})  
@Html.TextBoxFor(m => m.AssistName , new { id = "MyTxt" , @class = "form-control" })

<script type="text/javascript">
    function valueChanged() {
        if ($('#Mychk').is(":checked"))
            $("#MyTxt").show();
        else
            $("#MyTxt").hide();
    }
</script>

Edit

For show or hide in page load you need add this code:

$(document).ready(function() {
    valueChanged();
});
like image 178
Ali Soltani Avatar answered Jan 20 '26 22:01

Ali Soltani