[英]jQuery - Calling .change(); in javascript code does not fire
In this question, all code has been generalized. 在这个问题中,所有代码都已被概括。
As the question title has explained, I have a jQuery function: 正如问题标题所解释的,我有一个jQuery函数:
$(document).ready(function() {
if (something) {
// This does something
} else if (something else) {
// This does something else
} else {
// And this does something else
}
});
In this function, when the page loads, it checks for information that's on the web page. 在此功能中,页面加载时会检查网页上的信息。
I have a hidden input with a value that I check. 我有一个隐藏的输入,我检查了一个值。 This works.
这可行。
I have 2 Drop down lists with two IDs (for the sake of argument): ddl1, ddl2 我有2个具有两个ID(出于参数考虑)的下拉列表:ddl1,ddl2
In MVC 4, they will look like this: 在MVC 4中,它们将如下所示:
@Html.DropDownListFor(model => model.SelectedValue, Model.List1, new { id = "ddl1" })
@Html.DropDownListFor(model => model.SelectedValue, Model.List2, new { id = "ddl2" })
Next, I have two On Change jQuery methods, accordingly: 接下来,相应地,我有两个On Change jQuery方法:
$("#ddl1").change(function () {
// Does something when the index of the first drop down changes
});
$("#ddl2").change(function () {
// Does something when the index of the second drop down changes
});
When I change the index on the page, these methods fire. 当我更改页面上的索引时,这些方法将触发。 Now, Here's where my question comes into play.
现在,这是我的问题起作用的地方。
In my $(document).ready()
function, I call those On Change function from this method, it does not fire... 在我的
$(document).ready()
函数中,我从此方法调用那些On Change函数,它不会触发...
$(document).ready(function() {
if (something) {
if (condition for ddl1) {
$("#ddl1").change(); // Does not fire.
} else {
$("#ddl2").change(); // Does not fire.
}
} else if (something else) {
// This does something else
} else {
// And this does something else
}
});
To me, this makes no sense why those functions are not firing. 对我来说,为什么这些功能没有触发是没有意义的。
It's a direct method call. 这是直接方法调用。
If someone can shed some light on this matter, i'll be thankful. 如果有人能对此事有所启发,我将不胜感激。
Cheers. 干杯。
The proper way to trigger a change method using jQuery, is using trigger function, this way: 使用jQuery触发更改方法的正确方法是使用触发函数,这种方式是:
$(document).ready(function() {
if (something) {
if (condition for ddl1) {
$("#ddl1").trigger("change"); // TRIGGER change event
} else {
$("#ddl2").trigger("change"); // TRIGGER change event
}
} else if (something else) {
// This does something else
} else {
// And this does something else
}
});
More info here: http://api.jquery.com/trigger/ 此处提供更多信息: http : //api.jquery.com/trigger/
I modified my code to do the following (I went with just plain javascript, since I don't have the time to figure out why this is an issue): 我修改了代码以执行以下操作(我只使用普通的javascript,因为我没有时间弄清楚为什么这是一个问题):
1) I removed my on change jQuery method to a plain javascript function: From: 1)我将on jQuery方法更改为普通的javascript函数:从:
$("#ddl1").change(function () { /*code*/ });
To: 至:
function changeDdl1 () { /*code*/ }
2) I took out the triggers to simply add a javascript call: From: 2)我取出了触发器,仅添加了一个JavaScript调用:发件人:
$("#ddl1").trigger("change");
To: 至:
changeDdl1();
3) I added the @onchange MVC 4 attribute to my Drop Down Lists: 3)我在下拉列表中添加了@onchange MVC 4属性:
@Html.DropDownListFor(model => model.SelectedValue, Model.List1,
new { id = "ddl1", @onchange = "changeDdl1();"})
Thank you everyone who has provided help and hints. 谢谢所有提供帮助和提示的人。
Cheers. 干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.