gpt4 book ai didi

jQuery $(this) 什么时候返回什么?

转载 作者:行者123 更新时间:2023-12-01 01:37:50 27 4
gpt4 key购买 nike

我有两个 div 元素,这些 div 元素各包含一个选择。

<div id="signup_dialog">
<select name="cb_country">
<option value="0"></option>
</select>
</div>

<div id="create_dialog">
<select name="cb_country">
<option value="0"></option>
</select>
</div>

当我更改每个 div 元素中的选择时,$(this) 返回 $("#signup_dialog")$("#create_dialog") (DIV),但我想使用 $(this) 获取选择元素。

换句话说,this 是父 div,而不是 select。如何在事件处理程序中获取选择?

JS代码:

$("select[name=cb_country]","#signup_dialog,#create_dialog").live("change",function(){
console.log("element",$(this));
});

最佳答案

$(this) 将引用调用更改函数的任何元素。该功能仅适用于选择吗?

$("select[name=cb_country]").live("change",function(){
console.log("element",$(this));
});

$(this).val() 将给出所选选项的值。

关于jQuery $(this) 什么时候返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8913616/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com