Javascript 检测在jquery中单击了哪个下拉框
我正在多个下拉框上执行相同的功能,下拉框的id为:assignee、status、priorityJavascript 检测在jquery中单击了哪个下拉框,javascript,jquery,Javascript,Jquery,我正在多个下拉框上执行相同的功能,下拉框的id为:assignee、status、priority $("#assignee , #status , #priority").change(function() { //text here } 但我想知道点击了哪个特定的下拉列表。有什么方法可以做到这一点吗?只需使用以下方法: $("#assignee, #status, #priority").change(function() { if (this.id === "assign
$("#assignee , #status , #priority").change(function() {
//text here
}
但我想知道点击了哪个特定的下拉列表。有什么方法可以做到这一点吗?只需使用以下方法:
$("#assignee, #status, #priority").change(function() {
if (this.id === "assignee") { ... }
});
只需使用以下命令:
$("#assignee, #status, #priority").change(function() {
if (this.id === "assignee") { ... }
});
用这个。在任何事件处理程序中,这都指向实际事件发生的元素:
$("#assignee , #status , #priority").change(function() {
var changedElementID = $(this).attr("id");
}
另一种选择是使用:
您还可以找到有关其他事件对象属性的更详细信息
请注意,
this
和e.target
都是DOM对象,而不是jQuery对象。因此,您需要像上面我的代码中那样用jquery包装它,以使用jquery函数。如果只需获取一个id-您可以直接使用this.id
或e.target.id
,这两个方法更快。使用此方法。在任何事件处理程序中,这都指向实际事件发生的元素:
$("#assignee , #status , #priority").change(function() {
var changedElementID = $(this).attr("id");
}
另一种选择是使用:
您还可以找到有关其他事件对象属性的更详细信息
请注意,
this
和e.target
都是DOM对象,而不是jQuery对象。因此,您需要像上面我的代码中那样用jquery包装它,以使用jquery函数。如果只是获取一个id-您可以直接使用this.id
或e.target.id
,后者更快。触发事件时,在事件处理程序函数中this
指触发事件的元素,因此:
$('#assignee, #status, #priority').change(function(e) {
// Use 'this' to refer to the element that triggered the event
console.log(this.id);
// will output assignee, status or priority depending on which changed
});
触发事件时,在事件处理程序函数
中,此
指触发事件的元素,因此:
$('#assignee, #status, #priority').change(function(e) {
// Use 'this' to refer to the element that triggered the event
console.log(this.id);
// will output assignee, status or priority depending on which changed
});
使用目标:
$("#assignee , #status , #priority").change(function(e) {
var changedElementID = $(e.target).attr("id");
}
使用目标:
$("#assignee , #status , #priority").change(function(e) {
var changedElementID = $(e.target).attr("id");
}
在绑定
时,将此
传递给函数并使用自己的代码
$("#assignee , #status , #priority").change(function(boxID) {
console.log($(boxID).val()); //Gives you the value of the selected drop down
//text here
}
在绑定
时,将此
传递给函数并使用自己的代码
$("#assignee , #status , #priority").change(function(boxID) {
console.log($(boxID).val()); //Gives you the value of the selected drop down
//text here
}
您确实应该直接访问
id
属性,而不是使用.attr()
函数(即使不必要地使用jQuery,它也应该是.prop()
),因此:this.id
或e.target.id
@AnthonyGrist同意。只是想强调一下,两者都指向常规DOM对象,应该使用jQuery包装以获得其功能。更新了我的答案以使其更加清晰。如果在这里没有必要使用e.target
,那么这个
就足够了。你真的应该直接访问id
属性,而不是使用.attr()
函数(即使不必要地使用jQuery,它也应该是.prop()
),所以:this.id
或e.target.id
@AnthonyGrist同意。只是想强调一下,两者都指向常规DOM对象,应该使用jQuery包装以获得其功能。更新了我的答案,使其更加清晰。如果在e.target
中没有必要,那么这个
就足够了。这个答案不正确。传递给事件处理程序函数的第一个参数是事件本身,不管您如何调用它。因此,匿名函数中的boxID
是事件,而不是触发change
事件的元素。听起来似乎你在提倡使用内联事件处理程序属性(onchange=“foo()”
,等等),这是一种非常糟糕和过时的做法。这个答案不正确。传递给事件处理程序函数的第一个参数是事件本身,不管您如何调用它。因此,匿名函数中的boxID
是事件,而不是触发change
事件的元素。听起来似乎你在提倡使用内联事件处理程序属性(onchange=“foo()”
,等等),这是一种非常糟糕和过时的做法。