Javascript JQuery-删除html元素时重新格式化数字顺序
我目前正试图获得一个基本的功能,但工作,我说有5行,每个有自己的号码Javascript JQuery-删除html元素时重新格式化数字顺序,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我目前正试图获得一个基本的功能,但工作,我说有5行,每个有自己的号码 因此,我有5行……5,4,3,2,1以下是导致该行为的原因: var ordernum = $(this).text(); console.log(ordernum); if (ordernum !== 1) { $(this).text(parseInt($(this).text()) - 1); } 在本例中,ordernum是1。因此必须将其转换为int,就像在if子句中一样 var ordernum = pars
因此,我有5行……5,4,3,2,1以下是导致该行为的原因:
var ordernum = $(this).text();
console.log(ordernum);
if (ordernum !== 1) {
$(this).text(parseInt($(this).text()) - 1);
}
在本例中,ordernum
是1。
因此必须将其转换为int,就像在if子句中一样
var ordernum = parseInt($(this).text());
console.log(ordernum);
if (ordernum !== 1) {
$(this).text(ordernum - 1);
}
请查看下面的代码片段。 我对号码的重新分配做了如下更改
var ordernum = 1;
$(".contactspan").each(function(index) {
$(this).text(ordernum);
ordernum++;
});
首先将订单号指定为1,然后逐渐增加更多行
jQuery(函数($){
var反触点=0;
var counternum=0;
$(“#添加联系人”)。在('click',function()上{
反接触++;
$(“#contact_num”).append(“+countercontact+”.5102030405060\Delete”);
如果(反向触点===1){
$(“.deletecontact”).addClass('disabled');
}否则{
$(“.deletecontact”).removeClass('disabled');
}
});
$(“#contact_num”)。在(“单击”,“删除联系人”,函数()上{
if(countercontact您可以编写一个函数来更正数字,并在需要时运行,比如在删除项目之后
function correctIndex(){
$('#contact_num .row').each(function(){
$(this).find('.contactspan').html($(this).index()+1);
});
}
或者您只需更改代码的这一部分:
$(".contactspan").each(function(index) {
var ordernum = $(this).text();
console.log(ordernum);
if (ordernum !== 1) {
$(this).text(parseInt($(this).text()) - 1);
}
});
致:
问题太长。你的意思是你想删除最后一个(即,在你的情况下是5个)认为3已删除。我刚刚用我答案中的最后一行代码更新了小提琴。请检查。回答正确!我选择了最后一个解决方案:)
$(".contactspan").each(function(){
$(this).html($(this).closest('.row').index() + '.')
});