Javascript 如何使用带有 jQuery 的 change() 方法的参数的函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4897368/
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
How to use a function that takes arguments with jQuery's change() method?
提问by Manos Dilaverakis
I'm using jQuery version 1.5. I am looking at jQuery's change()function and specifically at this bit:
我正在使用 jQuery 1.5 版。我正在查看 jQuery 的change()函数,特别是在这一点上:
.change( [ eventData ], handler(eventObject) )
eventData: A map of data that will be passed to the event handler.
handler(eventObject): A function to execute each time the event is triggered.
What exactly is a "map of data" in JavaScript? How can I use the following test function as an event handler?
JavaScript 中的“数据映射”究竟是什么?如何使用以下测试函数作为事件处理程序?
var myHandler = function(msg){alert(msg);};
I've tried this:
我试过这个:
$("select#test").change(["ok"], myHandler);
and the alert reports [object Object]
和警报报告 [object Object]
回答by Felix Kling
See event.data
. The data is not passed as argument to handler, but as property of the event object:
见event.data
。数据不是作为参数传递给处理程序,而是作为事件对象的属性:
$("select#test").change({msg: "ok"}, function(event) {
alert(event.data.msg);
});
The handler always only accepts one argument, which is the event
object. This is the reason why your alert shows "[object Object]"
, your function is printing the event object.
If you want to use functions with custom arguments, you have to wrap them into another function:
处理程序始终只接受一个参数,即event
对象。这就是为什么您的警报显示"[object Object]"
,您的函数正在打印事件对象的原因。
如果要使用带有自定义参数的函数,则必须将它们包装到另一个函数中:
$("select#test").change({msg: "ok"}, function(event) {
myHandler(event.data.msg);
});
or just
要不就
$("select#test").change(function(event) {
myHandler("ok");
});
Btw. the selector is better written as $('#test')
. IDs are (should be) unique. There is no need to prepend the tag name.
顺便提一句。选择器最好写成$('#test')
. ID 是(应该)唯一的。无需预先添加标签名称。
回答by T.J. Crowder
What exactly is a "map of data" in Javascript?
Javascript 中的“数据映射”究竟是什么?
Basically just an object, e.g.:
基本上只是一个对象,例如:
var data = {
foo: "I'm foo",
bar: "I'm bar"
};
All JavaScript objects are essentially maps (aka "dictionaries" aka "associative arrays").
所有 JavaScript 对象本质上都是映射(又名“字典”又名“关联数组”)。
How can I use the following test function as an event handler?
如何使用以下测试函数作为事件处理程序?
By wrapping it in another function:
通过将其包装在另一个函数中:
$("select#test").change(function() {
myHandler($(this).val());
});
That calls myHandler
with the value of the select box whenever it changes.
myHandler
每当它改变时,就会调用选择框的值。
If you want to use the eventData
part, add an object prior to the handler:
如果要使用该eventData
部件,请在处理程序之前添加一个对象:
$("select#test").change({
foo: "I'm foo"
}, function(event) {
myHandler(event.data.foo, $(this).val());
});
That calls myHandler
with the "I'm foo" as the first argument, then the value of the select box, whenever it changes.
调用myHandler
时将“I'm foo”作为第一个参数,然后是选择框的值,只要它发生变化。