【问题标题】:If radio button is selected show alert如果选择单选按钮,则显示警报
【发布时间】:2015-08-16 02:00:16
【问题描述】:

我希望在单击单选按钮时显示警报。

我的单选按钮:

<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>

因此,如果选择了新单选按钮,我希望提醒说“新”,否则什么都没有。

通常这很容易,因为您有身份证,但在这种情况下没有身份证?

因为这是 RadioButtonFor。

谢谢

新代码

完整代码:

       $('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");

                    }

                });


            }

        });

这可行,但是如果单选按钮在页面加载时它不起作用,我必须单击另一个单选按钮,然后单击“新建”,然后它就会起作用..

如果它已经检查而不是检查时,它的任何工作机会。

【问题讨论】:

    标签: javascript jquery asp.net-mvc if-statement radio-button


    【解决方案1】:

    @Html.RadioButtonFor 使用nameid 属性创建一个正确的input

    您可以通过这种方式访问​​该值:

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

    不确定生成的收音机名称,因为它是一个内部变量。
    渲染页面并查看生成的名称。这就是我的情况。

    现在,关于您的警报。你需要监听change事件。

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

    为什么是“1”?因为您已经在 ASP.NET 代码中设置了它。 这就是它应该的样子。

    <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>
    

    当然,这一切都可以不使用 jQuery 来实现。

    【讨论】:

    • 但是如何确定选择了哪个单选按钮,我想要选择新按钮。
    • @mali 哦,忘记了主要问题。已编辑。
    • 快速问题,在页面加载时,我还能收到警报吗,因为通常选择新的,当我选择另一个单选按钮然后重新选择新时,警报会出现,但我想要选择下一个时的警报(在页面加载时)?
    • @mali 你不能确定new 在页面加载时总是被选中。例如,浏览器具有autocomplete 功能等。您可以在页面加载后检查它是否被检查。我现在将编辑我的答案。
    猜你喜欢
    • 2015-07-28
    • 2021-05-07
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 2015-07-17
    • 1970-01-01
    相关资源
    最近更新 更多