简体   繁体   English

如何在javascript中的函数内使用具有类名的变量?

[英]How to use a variable with class name inside a function in javascript?

I have a variable x which counts the number of fields added to a form dynamically.我有一个变量 x ,它计算动态添加到表单中的字段数。 I want to remove a field for which I tried to use the following code.我想删除我尝试使用以下代码的字段。 But I can't.但我不能。

$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $( "div" ).remove( ".org-details\'+(x-1)+\'" );x--;
    })
});

I want the output be something like this on runtime.我希望在运行时输出是这样的。

$( "div" ).remove( ".org-details1" );

Please help.请帮忙。

您应该使用模板文字(注意反引号):

$("div").remove(`.org-details${x-1}`)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM