简体   繁体   English

jQuery验证选择器ID更改

[英]JQuery verify selector ID on change

$('select').change(function() {
   //important stuff
});

Before doing important stuff I want to verify the selector in question by its ID. 在做important stuff之前,我想通过选择器的ID来验证选择器。 All of the IDs I want to affect end with _stat . 我要影响的所有ID _stat How would you do this with a regular expression (or something else) in JQuery? 您将如何在JQuery中使用正则表达式(或其他方式)执行此操作?

$('select').change(function() {
   if ($("select[id*='_stat']")) {  //<~ this aint work
   //important stuff
   }
});

You can do this with if ($(this).is("select[id$='_stat']")) , but it would surely be better to attach the event handler to these elements only in the first place -- then you wouldn't have to check at all: 您可以使用if ($(this).is("select[id$='_stat']"))做到这一点,但是一定要首先将事件处理程序附加到这些元素上肯定会更好-然后您根本不需要检查:

$("select[id$='_stat']").change(function() {
   //important stuff
});

You're looking for this: 您正在寻找:

if($(this).is('[id$="_stat"]')) {
    // important stuff
}

Although if you only want the change handler to run on those select elements with ids that end in _stat , you should check out Jon's answer. 尽管如果只希望change处理程序在ID以_stat结尾的select元素上运行,则应检查Jon的答案。

You could even only attach to selects with id ending in _stat: 您甚至可以只附加ID以_stat结尾的选择:

$('select[id$="_stat"]').change(function() {
   //important stuff only for selects id ending in _stat
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM