在当前元素的onchange上发送$(this) [英] Sending $(this) on an onchange on the current element

查看:682
本文介绍了在当前元素的onchange上发送$(this)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个HTML

<select class="category" style="margin-bottom: 5px;" onchange="getProducts('standard_product');">

并且您可以看到onchange调用getProducts函数。我想知道是否有办法发送此类似

and as you can see the onchange calls the getProducts function. I want to know if there is a way to sent in this like

<select class="category" style="margin-bottom: 5px;" onchange="getProducts('standard_product', $(this));">

我希望与当前选择相关联

which i would hope would be associated to the current select

推荐答案

如果你试图在你的函数中设置这个的值,你可以使用 .call

If you're trying to set the value of this in your function, you can use .call:

onchange="getProducts.call(this, 'standard_product');"

现在在你的getProducts函数中,这个将是收到活动的元素。

Now in your getProducts function, this will be the element that received the event.

function getProducts( prod ) {

    alert( this );  // the <select> element

}

你也可以传递事件对象:

onchange="getProducts.call(this, 'standard_product', event);"

...并在您的函数中引用它:

...and reference it in your function:

function getProducts( prod, e ) {

    alert( this );  // the <select> element

    alert( e.type );  // the event type

}






编辑:正如 @Cyber​​nate 所述,这是将DOM元素设置为。你需要将它包装在你的 getProducts 函数 $(this)中,或者在你的内联中设置它处理程序。


As noted by @Cybernate, this is setting the DOM element to this. You'll need to wrap it in your getProducts function $(this), or set it as such in your inline handler.

虽然设置这个到元素本身更符合典型的事件处理程序行为。

Though setting this to the element itself is more in line with typical event handler behavior.

编辑:进一步解释 .call 是的,它允许你手动在你正在调用的函数中设置这个的值。

To further explain what .call does, it allows you to manually set the value of this in the function you're calling.

使用此功能,只需提醒

Take this function, which simply alerts this:

function some_func() {

    alert( this );

}

以基本方式(在浏览器中)调用它引用DOM窗口。

Calling it in a basic manner (in a browser) makes this reference the DOM Window.

some_func();  // the alert will be DOM Window

但现在允许使用 .call调用,并将第一个参数设置为 123

But now lets invoke using .call, and setting the first argument to 123.

some_func.call( 123 );  // the alert will be 123

现在警报显示 123 。该函数没有改变,但这个的值有,因为我们使用 .call 手动设置它。

You can see that now the alert shows 123. The function hasn't changed, but the value of this has because we've manually set it using .call.

如果您有其他参数要发送,只需将它们放在 thisArg 之后。

If you have additional arguments to send, you just place them after the thisArg.

function some_func( arg1 ) {

    alert( this );
    alert( arg1 );

}

some_func.call( 123, 456 );

提醒将为 123 ,您发送的下一个参数将设置为 arg1 参数,因此 arg1 456

The this alert will be 123, and the next argument you send will be set to the arg1 parameter, so arg1 will be 456.

所以你可以看到电话基本上切掉你发送的第一个参数,将其设置为 this 的值,并将剩余的参数设置为与函数参数关联的正常参数。

So you can see that call basically slices off your first argument you send, sets it as the value of this, and sets the remaining arguments as your normal arguments associated with your function parameters.

这篇关于在当前元素的onchange上发送$(this)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆