4

HTML

<select id="edit-attributes-1"></select>
<select id="edit-attributes-2"></select>
<select id="edit-attributes-3"></select>
<select id="edit-attributes-4"></select>

这是由 cms 生成的,我无能为力

查询:

 $('select[id^="edit-attributes-"][id!="edit-attributes-12"]').after('<span class="step stepdown step-'+$(this).attr('id')+'">+</span>');  

所以我创建了这个,因为尝试 jquery.ui.spinner 在识别元素方面存在问题。

笔记:

$(this).attr('id') here results as unknown.

结果

<select id="edit-attributes-1"></select>
   <span class="step stepdown step-**unknown**">+</span>
<select id="edit-attributes-2"></select>
   <span class="step stepdown step-**unknown**">+</span>
<select id="edit-attributes-3"></select>
   <span class="step stepdown step-**unknown**">+</span>
<select id="edit-attributes-4"></select>
   <span class="step stepdown step-**unknown**">+</span>

如您所见, $(this).attr('id') 的部分是未知的。

期望的结果

<select id="edit-attributes-1"></select>
   <span class="step stepdown step-**edit-attributes-1**">+</span>
<select id="edit-attributes-2"></select>
   <span class="step stepdown step-**edit-attributes-2**">+</span>
<select id="edit-attributes-3"></select>
   <span class="step stepdown step-**edit-attributes-3**">+</span>
<select id="edit-attributes-4"></select>
   <span class="step stepdown step-**edit-attributes-4**">+</span>

我不知道我怎样才能实现这种概念,或者请提出一种简化事情的最佳方法。

4

2 回答 2

2

this在您的代码中没有引用您选择的元素,您可以使用after's 功能:

$('select[id^="edit-attributes-"][id!="edit-attributes-12"]').after(function(ind){
   return '<span class="step stepdown step-'+ this.id +'">+</span>'
}); 

http://jsfiddle.net/qjVHM/

于 2012-10-10T05:57:38.060 回答
0

试试这个代码:

 $('select[id^="edit-attributes-"][id!="edit-attributes-12"]').each(function () {
    var this_selection = $(this);
    var selection_id = this_selection.attr('id');
    this_selection.after('<span class="step stepdown step-' + selection_id + '"></span>');  
 });
于 2012-10-10T06:00:43.350 回答