gpt4 book ai didi

javascript - 多重选择与多重 this

转载 作者:行者123 更新时间:2023-11-30 17:20:56 27 4
gpt4 key购买 nike

需要有关 Jquery 多选的帮助。

这是代码

$.each(($("#reciprocitationAgreement option:selected:not([disabled]), #reciprocitationAgreement input")), function() {
$(this).text();
});

有两种类型的元素通过每个循环。1 - 选择(选项) - 从选项元素中获取文本2 - 输入字段 - 从输入字段获取

有 2 个选定元素。有没有办法在同一代码块中指向 $(this, option).text();$(this, input).val()

最佳答案

这很简单,您可以在同一个函数中完成,不需要两个函数。只需检查元素的 tagName 是否为 INPUT 然后相应地操作:

$.each(($("#reciprocitationAgreement option:selected:not([disabled]), #reciprocitationAgreement input")), function() {
if (this.tagName == 'INPUT') {
$(this).val(); // ...
} else {
$(this).text(); // ...
}
});

关于javascript - 多重选择与多重 this,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25145691/

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