【问题标题】:Display the alert box when change the @html.DropDownList value更改 @html.DropDownList 值时显示警告框
【发布时间】:2014-05-22 08:39:23
【问题描述】:
  @Html.DropDownListFor(model => model.Status, new List<SelectListItem> 
       { new SelectListItem{Text="Active", Value="1",Selected =true},
         new SelectListItem{Text="Deactive", Value="0"}})

如果我将值 Active 更改为 De active,则显示一个警报框。如何显示警告框。

【问题讨论】:

  • 离题:如果您想要与活动相反的内容,请使用“非活动”或“被动”,甚至可能根据上下文使用“空闲”。但据我所知,Deactive 不是一个有效术语。

标签: c# javascript asp.net-mvc-4 razor


【解决方案1】:

您可以使用 Jquery 中的 change() 处理程序来监听事件。

$( "#targetId").change(function() {
     alert( "Something changed handle it here" );
});

http://api.jquery.com/change/

【讨论】:

  • 我是 asp.net mvc 的新手。请告诉如何从 dropdowenlistfor 调用 jquery 函数
  • 将此 javascript 代码添加到您的 .cshtml 文件中的 &lt;script&gt; 块中。
  • 阅读这个:asp.net/mvc/tutorials/mvc-4/bundling-and-minification 非常有趣的信息 :)
  • new {@id="targetId"} 并且这样调用脚本函数是对的
  • 没错,#targetId,指的是&lt;select&gt;的id,你可以在它的定义中使用{@id="targetId"}将其定义为可选参数
【解决方案2】:
  Razor:-


 @Html.DropDownListFor(model => model.Status, new List<SelectListItem> 
               { new SelectListItem{Text="Active", Value="1",Selected =true},
                 new SelectListItem{Text="Deactive", Value="0"}})

Jquery(当您更改其 id 在下面的查询中附加的下拉列表值时调用更改事件):-

 <script>

    $(document).ready(function(){
        $('select#status').change(function() {
            alert("value changed. New value is " + $(this).val());
    });
    });
});

</script>

【讨论】:

  • 请等待@neel 我是 mvc 新手
  • cshtml 页面:@Html.DropDownListFor(model => model.Status, new List { new SelectListItem{Text="Active", Value="1",Selected =true}, new SelectListItem{Text="Deactive", Value="0"},new {@id="ddlTMManagers"}}) 并放置脚本代码: $(function() { $('select#ddlDMManagers').change(function( ) { alert("值改变了。新值为 " + $(this).val()); });
  • 我收到此错误:集合的最佳重载添加方法 'System.Collections.Generic.List.Add(System.Web.Mvc.SelectListItem)'初始化程序有一些无效参数
  • 这是因为语法错误而发生的。尝试我更新的答案@satheeshkumarMCA
【解决方案3】:

将此代码添加到您的主布局或下拉菜单所在的视图中:

第一种方式:

jQuery 代码:

<script>

    $(document).ready(function(){

     $('select#status').change(function() {
            alert($(this).val());
    });
});

</script>

第二种方式:

或者您可以像这样添加自己的 id:

 @Html.DropDownListFor(model => model.Status, new List<SelectListItem> 
               { new SelectListItem{Text="Active", Value="1",Selected =true},
                 new SelectListItem{Text="Deactive", Value="0"}
               },
                 null,
                 new {@id="DDLStatus"})

和脚本:

  <script>

        $(document).ready(function(){

         $('select#DDLStatus').change(function() {
                alert($(this).val());
        });
    });

    </script>

注意:确保 jquery 脚本文件包含在您的主布局中,主要是在 View --> Shared --> _Layout.cshtml

【讨论】:

  • {@id="DDLStatus"})}) 在这个额外的你放“)”这个符号
  • 我遇到了这种错误。最好重载的 Add 方法 'System.Collections.Generic.List.Add(System.Web.Mvc.SelectListItem)'集合初始化器有一些无效参数
  • 我错过了关闭 SelectListItem 列表的 } 现在已修复
  • 在我的cshtml页面中:@Html.DropDownListFor(model => model.Status, new List { new SelectListItem{Text="Active", Value="1",Selected =true} , new SelectListItem{Text="Deactive", Value="0"},new {@id="DDLStatus"}})
  • 应该是错的:@Html.DropDownListFor(model =&gt; model.Status, new List&lt;SelectListItem&gt; { new SelectListItem{Text="Active", Value="1",Selected =true}, new SelectListItem{Text="Deactive", Value="0"} }, null, new {@id="DDLStatus"})
猜你喜欢
  • 2012-08-12
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
  • 2023-03-15
  • 2021-03-25
  • 2017-05-11
  • 2022-07-01
  • 1970-01-01
相关资源
最近更新 更多