简体   繁体   English

更改@ html.DropDownList值时显示警报框

[英]Display the alert box when change the @html.DropDownList value

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

If i change the value Active to De active display the one alert box. 如果我将“有效”值更改为“无效”,则会显示一个警报框。 How to display the alert box. 如何显示警报框。

You could use the change() handler in Jquery to listen for the event. 您可以在Jquery中使用change()处理函数来监听事件。

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

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

  Razor:-


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

Jquery(Change event is called when you change the dropdownlist value whose id is attached in below query) :- jQuery(更改ID在以下查询中附加的dropdownlist值时,将调用Change事件):-

 <script>

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

</script>

Add this code in your master layout or in view in which Dropdown is: 将此代码添加到您的主布局或下拉列表位于的视图中:

First Way: 第一种方式:

Jquery CODE: jQuery代码:

<script>

    $(document).ready(function(){

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

</script>

Second Way: 第二种方式:

Or you can add your own id like this: 或者您可以添加自己的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"})

and script: 和脚本:

  <script>

        $(document).ready(function(){

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

    </script>

Note: make sure that jquery script file is included in your master layout mostly it is in View --> Shared --> _Layout.cshtml 注意:确保jquery脚本文件包含在您的主布局中,主要是在视图->共享-> _Layout.cshtml中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM