Jquery:从触发器返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9145347/
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
Jquery: Returning Value From Trigger
提问by Petty_Crim
I am using a trigger call a get a value from custom event but I want it to return a value and its only giving me Object Object when I do the following call:
我正在使用触发器调用从自定义事件中获取值,但我希望它返回一个值,并且在我执行以下调用时它只给我对象对象:
var user_id=$("#my_div").trigger("get_id", [username]);
My trigger event function looks like this:
我的触发事件函数如下所示:
$("#my_div").on("get_id", function(e, username){
var user_id;
if (username='fred'){
user_id=1;
}
else if(username='mario'){
user_id=2;
}
return user_id;
});
回答by nicosantangelo
You cannot return a value from a trigger, but you can store information in many different ways, one way is using a object as a parameter:
您不能从触发器返回值,但您可以通过多种不同方式存储信息,一种方式是使用对象作为参数:
//event
$("element").on("click", function(event, informationObj) {
informationObj.userId = 2; //you have to access a propery so you can modify the original object
});
//trigger
var informationObj = {userId : 0};
$("element").trigger("click", [informationObj ]); //informationObj.userId === 2
other way is using jQuerys .data()
method
另一种方法是使用 jQuerys.data()
方法
//event
$("element").on("click", function() {
$(this).data("userId", 2);
});
//trigger
$("element").trigger("click").data("userId") //2
Another thing you can do is modifying a variable that's declared outside the event and then using it after calling the trigger, orstoring it as a property in the element that has the event with the this
keyword like this:
您可以做的另一件事是修改在事件外部声明的变量,然后在调用触发器后使用它,或者将其作为属性存储在具有事件的元素中,this
关键字如下:
//inside the event function
this.userId = 2;
//outside the event
$("element").trigger("click").get(0).userId
Hope it helps.
希望能帮助到你。
Edit:
编辑:
Also, take a look at @Arm0geddon answerbelow, using .triggerHandler()
, just beware that it has some side effects, like not bubbling up the DOM hierarchy.
另外,看看下面的@Arm0geddon 答案,使用.triggerHandler()
,只是要注意它有一些副作用,比如不会冒泡 DOM 层次结构。
回答by Arm0geddon
What you can do is use .triggerHandler()
instead of .trigger()
. This will return a value.
您可以做的是使用.triggerHandler()
而不是.trigger()
. 这将返回一个值。
var user_id=$("#my_div").triggerHandler("get_id", [username]);
回答by Julien Lafont
A trigger cannot return a response, because it's a callback method.
触发器不能返回响应,因为它是一个回调方法。
In addition jQuery have a fluid API, so .trigger()
returns always $(this)
.
You can write $("#my_id").trigger(something).show().on(someelse)...
此外 jQuery 有一个流畅的 API,所以.trigger()
总是返回$(this)
.
你可以写$("#my_id").trigger(something).show().on(someelse)...
回答by gdoron is supporting Monica
The trigger
function doesn't return the value you return from the event handler.
该trigger
函数不会返回您从事件处理程序返回的值。
It returns jQuery object
...
它返回jQuery object
...
.trigger( eventType [, extraParameters] ) Returns: jQuery
.trigger( eventType [, extraParameters] ) 返回:jQuery
This was designed so you could write things like this:
这是为了你可以写这样的东西:
$("#my_div").trigger("get_id", [username]).val('foo').css('color', 'red');
回答by PretorDH
Try this:
尝试这个:
$("#my_div").on("get_id", function(e, username){
var user_id;
if (username='fred'){
user_id=1;
}
else if(username='mario'){
user_id=2;
}
return user_id;
});
var user_id=$("#my_div").triggerHandler("get_id", ["username"]);
回答by MistereeDevlord
triggerHandler is the correct answer, but it's worth noted as I have not seen it here yet... You can pass another callback along as well, and have your "on" function call the passed callback to set some return value etc. assuming it's better not to wait for some reason.
triggerHandler 是正确的答案,但值得注意的是,我还没有在这里看到它......你也可以传递另一个回调,并让你的“on”函数调用传递的回调来设置一些返回值等。假设它是最好不要因为某种原因等待。