jquery选择具有动态ID的选项列表

我有一个带有id的选项列表,有时可能是其中之一:

edit-panes-billing-billing-zone edit-panes-billing-billing-zone--1 edit-panes-billing-billing-zone--2 edit-panes-billing-billing-zone--3 edit-panes-billing-billing-zone--4 

一次只有一个选择元素。

是否可以使用jQuery动态选择选项列表?
这是我尝试过的,但它不起作用:

 $("select[id^='edit-panes-billing-billing-zone--']").each(function() { alert('test'): } 

您的代码中有许多失败的语法错误:

 alert('test'): 

应该

 alert('test'); 

 alert('test'): } 

应该

 alert('test'); }); 

最终结果应该是:

 $("select[id^='edit-panes-billing-billing-zone--']").each(function() { alert('test'); }); 

这在我运行的所有测试中都成功了。

尝试

 $(document).ready(function() { $('select option[id^="edit-panes-billing-billing-zone--"]').each(function(){ alert($(this).text()); }); }); 

例如,为每个选择提供属性类

      

然后代码是(按类获取元素):

 $(document).ready(function() { $(".my-select").each(function() { alert('test'); }); }); 

你的问题不是很清楚I have an option list with an id

option list ,我相信这些是一堆标签。

 $("select[id^='edit-panes-billing-billing-zone--']").change(function() { alert('test'): }); 

如果我们使用您的代码,那么您应该尝试这种方式:

 $("select[id^='edit-panes-billing-billing-zone--']").each(function() { $(this).change(function(){ alert('test'); }); }); 

注意:

你的代码丢失了}); 在结束时,您的警报以semicolon ';'结束colon-> :不是semicolon ';'

.each()尝试.change()事件。