New:@Html.RadioButt...">

If radio window is selected

I need to show a warning when I press a switch.

My switches:

<div class="genericFormField">
   New:@Html.RadioButtonFor(m => m.Form.InstallType, 1, Model.InstallationHeader.InstallType == 1 ? new { Checked = "checked" } : null )
   Pool:@Html.RadioButtonFor(m => m.Form.InstallType, 2, Model.InstallationHeader.InstallType == 2 ? new { Checked = "checked" } : null)
   Refurb:@Html.RadioButtonFor(m => m.Form.InstallType, 3, Model.InstallationHeader.InstallType == 3 ? new { Checked = "checked" } : null)              
</div>

      

So, if a new radio button is selected, I want the alert to say "NEW", otherwise nothing.

Usually this is easy because you have an ID but no ID for that?

Since this is RadioButtonFor.

thank

New code

Complete code:

       $('input[name="Form.InstallType"]').on('change', function() {
            var val = $('input[name="Form.InstallType"]:checked').val();
            if (val === '1') {

                var validOptions = "@Url.Action("SerialProdNumStockSiteAutoComplete", "Ajax")?stocksitenum=LW&model=" + $("#Form_Prod_Num").val();
                previousValue = "";

                $('#abc').autocomplete({
                    autoFocus: true,
                    source: validOptions,

                }).change(function(){

                    var source = validOptions;
                    var found = $('.ui-autocomplete li').text().search(source);
                    console.debug('found:' + found);
                    var val = $('input[name="Form.InstallType"]:checked').val();

                    if (found < 0 && val === '1') {
                        $(this).val('');
                        alert("Please choose from auto complete");

                    }

                });


            }

        });

      

This works, however, if the switch is on page load, it doesn't work, I have to press another radio button and then press "new" and then it will work.

any chance it works if it's already checked, not when checked.

+3


source to share


1 answer


@ Html.RadioButtonFor creates correct attributes input

with name

and attributes id

.

You can access this value like this:

$('input[name="Form.InstallType"]:checked').val();

      

Not sure about the name of the created radio as it is an internal variable.
Display the page and look at the generated name. This is how it looks in my case.

Now about your warning. You need to listen to the change

event.



$('input[name="Form.InstallType"]').on('change', function()
{
    var val = $('input[name="Form.InstallType"]:checked').val();        
    if (val === '1') alert('New');
});

      

Why "1"? Because you have installed it in ASP.NET code. This is how it should look.

<div class="genericFormField">
   New: @Html.RadioButtonFor(m => m.Form.InstallType, 'new')
   Pool: @Html.RadioButtonFor(m => m.Form.InstallType, 'pool')
   Refurb: @Html.RadioButtonFor(m => m.Form.InstallType, 'refurb')              
</div>
<script>
    $(document).ready(function() {
        AlertIfNewIsSelected(); // Check if it is selected on page load. According to the question in comments.
        $('input[name="Form.InstallType"]').on('change', AlertIfNewIsSelected);
    });

    function AlertIfNewIsSelected()
    {
        var val = $('input[name="Form.InstallType"]:checked').val();        
        if (val === 'new') alert('New');
    }
</script>

      

Of course, all of this can be achieved without using jQuery.

+3


source