ASP.NET MVC 下拉列表更改导致没有 Javascript 的回发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4510821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC Dropdown list on change to cause postback without Javascript
提问by 4imble
Is it possible to cause a postback to an action from changing your selection in a dropdown list without using JavaScript?
是否可以在不使用 JavaScript 的情况下通过更改下拉列表中的选择来导致操作回发?
I remember when using the ASP.NET Forms dropdown list control, there was a property that when set would cause a postback on change, did this work without JS? - If so, how?
我记得在使用 ASP.NET Forms 下拉列表控件时,有一个属性,当设置时会导致更改回发,这在没有 JS 的情况下工作吗?- 如果是这样,如何?
Many thanks, Kohan.
非常感谢,科汉。
回答by Scott
You can achieve the postback effect using jQuery. JavaScript in some form or another is required for automatic postbacks. Even ASP.NET uses JavaScript in the background to implement the AutoPostBack property.
您可以使用 jQuery 实现回发效果。自动回发需要某种形式的 JavaScript。甚至 ASP.NET 也在后台使用 JavaScript 来实现 AutoPostBack 属性。
<script type='text/javascript'\>
$('#dropDown').change(function () {
$(this).parents('form').submit();
});
</script>
回答by kemiller2002
You need JavaScript to automatically post back data. You don't have to write it, but you still need it. You can post without JavaScript (someone pressing a submit button), but not automatically.
您需要 JavaScript 来自动回发数据。你不必写它,但你仍然需要它。您可以在没有 JavaScript 的情况下发布(有人按下提交按钮),但不能自动发布。
I believe what you are thinking of is the AutoPostBackproperty.
我相信你在想的是AutoPostBack属性。
回答by Leniel Maccaferri
回答by Mandeep Janjua
Complete script which worked for me
对我有用的完整脚本
@Html.DropDownList("Projects", Model.Projects, new {
style = "Height: 25px; width: 225px;",
onchange = "$(this).parents('form').submit();" })

