用正则表达式替换 select 索引属性
replace select indexed attributes with regexp
我有以下 DOM :
<select id="evo_calculatorbundle_ruletype_assertions_0_logicalOperator">...</select>
<select id="evo_calculatorbundle_ruletype_assertions_1_logicalOperator">...</select>
使用 JqueryUI Sortable 组件,我正在对这些 select 标签进行排序。排序后,#1 select 排在#0 select 之前。
我想根据新的 DOM.
重新索引这些 select
这是我的可排序实例:
// Sortable assertions
$collectionHolder.sortable({
placeholder: "ui-state-highlight",
handle: ".handle",
helper: "original",
cursor: "move",
update: function(event, ui) {
var elements = $collectionHolder.find("tr");
var count = elements.length;
console.log(count);
$(elements).each(function(index) {
$(this).data('index', index).attr('data-index', index);
var pattern = '/_[0-9]+_/g';
var replacement = '_' + index + '_';
$(this).find('select').each(function(){
var before = $(this).attr('id');
var after = $(this).attr('id').replace(pattern, replacement);
console.log(before + " " + after);
$(this).attr('id', $(this).attr('id').replace(pattern, replacement));
$(this).css('border', '3px solid black');
});
});
}
});
替换不起作用,console.log(before + " " + after)
打印以下内容:
evo_calculatorbundle_ruletype_assertions_1_logicalOperator evo_calculatorbundle_ruletype_assertions_1_logicalOperator
evo_calculatorbundle_ruletype_assertions_0_logicalOperator evo_calculatorbundle_ruletype_assertions_0_logicalOperator
我预计
evo_calculatorbundle_ruletype_assertions_1_logicalOperator evo_calculatorbundle_ruletype_assertions_0_logicalOperator
evo_calculatorbundle_ruletype_assertions_0_logicalOperator evo_calculatorbundle_ruletype_assertions_1_logicalOperator
我在 replace
javascript 功能上做错了吗?还是我的模式不对?
javascript中的正则表达式是用斜杠括起来的,如果用单引号括起来,它就是一个字符串。
使用:
var pattern = /_[0-9]+_/g;
我有以下 DOM :
<select id="evo_calculatorbundle_ruletype_assertions_0_logicalOperator">...</select>
<select id="evo_calculatorbundle_ruletype_assertions_1_logicalOperator">...</select>
使用 JqueryUI Sortable 组件,我正在对这些 select 标签进行排序。排序后,#1 select 排在#0 select 之前。 我想根据新的 DOM.
重新索引这些 select这是我的可排序实例:
// Sortable assertions
$collectionHolder.sortable({
placeholder: "ui-state-highlight",
handle: ".handle",
helper: "original",
cursor: "move",
update: function(event, ui) {
var elements = $collectionHolder.find("tr");
var count = elements.length;
console.log(count);
$(elements).each(function(index) {
$(this).data('index', index).attr('data-index', index);
var pattern = '/_[0-9]+_/g';
var replacement = '_' + index + '_';
$(this).find('select').each(function(){
var before = $(this).attr('id');
var after = $(this).attr('id').replace(pattern, replacement);
console.log(before + " " + after);
$(this).attr('id', $(this).attr('id').replace(pattern, replacement));
$(this).css('border', '3px solid black');
});
});
}
});
替换不起作用,console.log(before + " " + after)
打印以下内容:
evo_calculatorbundle_ruletype_assertions_1_logicalOperator evo_calculatorbundle_ruletype_assertions_1_logicalOperator
evo_calculatorbundle_ruletype_assertions_0_logicalOperator evo_calculatorbundle_ruletype_assertions_0_logicalOperator
我预计
evo_calculatorbundle_ruletype_assertions_1_logicalOperator evo_calculatorbundle_ruletype_assertions_0_logicalOperator
evo_calculatorbundle_ruletype_assertions_0_logicalOperator evo_calculatorbundle_ruletype_assertions_1_logicalOperator
我在 replace
javascript 功能上做错了吗?还是我的模式不对?
javascript中的正则表达式是用斜杠括起来的,如果用单引号括起来,它就是一个字符串。
使用:
var pattern = /_[0-9]+_/g;