触发下拉更改事件 [英] Trigger change event of dropdown
问题描述
我想在 $(document).ready 中使用 jquery 触发 dropdown 的更改事件.
I want to trigger the change event of dropdown in $(document).ready using jquery.
我在用户详细信息页面中有一个国家和州的级联下拉菜单.如何使用 C# 在 MVC 中设置国家和州的值(根据用户 ID 从数据库中获取).
I have a cascading dropdown for country and state in user details page. how can i set the value (which is taken from DB based on the user id) for country and state in MVC with C#.
推荐答案
我对 JQuery 了解不多,但我听说它允许使用这种语法触发本机事件.
I don't know that much JQuery but I've heard it allows to fire native events with this syntax.
$(document).ready(function(){
$('#countrylist').change(function(e){
// Your event handler
});
// And now fire change event when the DOM is ready
$('#countrylist').trigger('change');
});
您必须在调用 trigger() 或 change() 之前声明更改事件处理程序,否则它不会被触发.感谢您提及@LenielMacaferi.
You must declare the change event handler before calling trigger() or change() otherwise it won't be fired. Thanks for the mention @LenielMacaferi.
更多信息这里.
这篇关于触发下拉更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!