Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery使用.after()在表中添加行 var停止指数=1; $('.add_stop')。单击(函数(){ tr='#停止'+停止索引 $(tr).之后(''+停止索引+''); stop_index++; });_Javascript_Html - Fatal编程技术网

Javascript jquery使用.after()在表中添加行 var停止指数=1; $('.add_stop')。单击(函数(){ tr='#停止'+停止索引 $(tr).之后(''+停止索引+''); stop_index++; });

Javascript jquery使用.after()在表中添加行 var停止指数=1; $('.add_stop')。单击(函数(){ tr='#停止'+停止索引 $(tr).之后(''+停止索引+''); stop_index++; });,javascript,html,Javascript,Html,这是代码(在表中添加一行) 但是,它不起作用 如果我写这样的代码,它就会工作 $('#stop1')。在(''+stop_index+'')之后 问题是什么?问题是在创建新行之后,而不是在新行之前,您正在增加停止索引。因此,当您添加第一个新行时,它将具有id=stop1,而不是id=stop2。下次尝试添加新行时,您将尝试在#stop2之后添加它,但该行不存在,因此不会发生任何事情 把线移开 var stop_index = 1; $('.add_stop').click(function()

这是代码(在表中添加一行)

但是,它不起作用

如果我写这样的代码,它就会工作

$('#stop1')。在(''+stop_index+'')之后


问题是什么?

问题是在创建新行之后,而不是在新行之前,您正在增加
停止索引。因此,当您添加第一个新行时,它将具有
id=stop1
,而不是
id=stop2
。下次尝试添加新行时,您将尝试在
#stop2
之后添加它,但该行不存在,因此不会发生任何事情

把线移开

var stop_index = 1;

$('.add_stop').click(function(){
        tr = '#stop' + stop_index
        $(tr).after('<tr id="stop'+ stop_index + '"><td>'+ stop_index +'</td></tr>');
        stop_index ++;
});
向上一行

stop_index++;
$('.add_stop')。单击(函数(){
tr='#停止'+停止索引
stop_index++;
$(tr).之后(''+停止索引+'');
});

问题在于,在创建新行之后,而不是在新行之前,您正在增加
停止索引。因此,当您添加第一个新行时,它将具有
id=stop1
,而不是
id=stop2
。下次尝试添加新行时,您将尝试在
#stop2
之后添加它,但该行不存在,因此不会发生任何事情

把线移开

var stop_index = 1;

$('.add_stop').click(function(){
        tr = '#stop' + stop_index
        $(tr).after('<tr id="stop'+ stop_index + '"><td>'+ stop_index +'</td></tr>');
        stop_index ++;
});
向上一行

stop_index++;
$('.add_stop')。单击(函数(){
tr='#停止'+停止索引
stop_index++;
$(tr).之后(''+停止索引+'');
});
你可以这样试试

$('.add_stop').click(function(){
    tr = '#stop' + stop_index
    stop_index ++;
    $(tr).after('<tr id="stop'+ stop_index + '"><td>'+ stop_index +'</td></tr>');
});
$('.add_stop')。单击(函数(){
tr='#停止'+停止索引;
//要生成uniq id,您需要添加新的tr
设newTrIDIndex=停止指数+1;
$(tr).之后(''+newdrindex+'');
停止指数=newtrindex;
});
如果您不使用加号,那么您将使用旧索引创建tr,这意味着您将使用两个具有相同id的tr结束。因此,为了避免在创建tr之前创建索引值。

您可以这样尝试

$('.add_stop').click(function(){
    tr = '#stop' + stop_index
    stop_index ++;
    $(tr).after('<tr id="stop'+ stop_index + '"><td>'+ stop_index +'</td></tr>');
});
$('.add_stop')。单击(函数(){
tr='#停止'+停止索引;
//要生成uniq id,您需要添加新的tr
设newTrIDIndex=停止指数+1;
$(tr).之后(''+newdrindex+'');
停止指数=newtrindex;
});

如果您不使用plus,那么您将使用旧索引创建tr,这意味着您将使用两个具有相同id的tr结束。因此,为了避免在创建tr之前创建索引值。

查看您的代码,您已经需要一个
元素才能使其工作,然后下一个元素还将添加id
stop1
。也许您应该更早地增加该值

var停止指数=1;
$('.add_stop')。单击(函数(){
tr='#停止'+停止索引
stop_index++;
$(tr).之后(''+停止索引+'');
});

1.
添加

查看您的代码,您已经需要一个
元素才能使其工作,然后下一个元素还添加了id
stop1
。也许您应该更早地增加该值

var停止指数=1;
$('.add_stop')。单击(函数(){
tr='#停止'+停止索引
stop_index++;
$(tr).之后(''+停止索引+'');
});

1.
添加
您将使用

$('.add_stop').click(function(){
   tr = '#stop' + stop_index;
   //to generate uniq id you need to plus it bore adding new tr
   let newTrIDIndex = stop_index + 1;
   $(tr).after('<tr id="stop'+ newTrIDIndex + '"><td>'+ newTrIDIndex +'</td></tr>');
    stop_index = newTrIDIndex ;
});
或者你也可以使用

$("#" + id + ).closest( "tr" ).after( ... );
这在本质上是等价的

有关详细信息,请参阅。

您将使用

$('.add_stop').click(function(){
   tr = '#stop' + stop_index;
   //to generate uniq id you need to plus it bore adding new tr
   let newTrIDIndex = stop_index + 1;
   $(tr).after('<tr id="stop'+ newTrIDIndex + '"><td>'+ newTrIDIndex +'</td></tr>');
    stop_index = newTrIDIndex ;
});
或者你也可以使用

$("#" + id + ).closest( "tr" ).after( ... );
这在本质上是等价的


有关详细信息,请参阅。

您的
stop\u index
变量是否在其他地方编辑过?您是否可以在jsfill共享代码?您的代码将导致多个具有相同id(stopx)的项目,这在html中是不允许的。在调用
.after()
之前,您需要执行
stop\u index++
,因此,新行将获得不同的索引。是否总是在最后一行之后追加行?是否在其他地方编辑了
stop\u index
变量?是否可以在jsfill共享代码?您的代码将导致具有相同id(stopx)的多个项,这在html中是不允许的。在调用
.after()之前,需要执行
stop\u index++
,因此新行将获得不同的索引。是否总是在最后一行之后追加行?