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生成的,我什么也做不了
JQUERY:
$('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>
我不知道如何才能实现这种概念,或者请提供一个如何简化事情的最佳方法。
答案 0 :(得分:2)
this
未引用您选择的元素,您可以使用after
的函数:
$('select[id^="edit-attributes-"][id!="edit-attributes-12"]').after(function(ind){
return '<span class="step stepdown step-'+ this.id +'">+</span>'
});
答案 1 :(得分: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>');
});