gpt4 book ai didi

javascript - 在 append() 中将 Javascript 变量传递给 HTML 代码

转载 作者:行者123 更新时间:2023-11-30 13:51:29 25 4
gpt4 key购买 nike

我想将一个变量传递给我要附加的 html 代码:

var max = 1;    
$(wraper).on("click", ".add_field_button", function(e){ //on add input click
e.preventDefault();
if(max < max_fields){ //max input box allowed
max++;
console.log(max); // max now is equal to 2
$(wrapper).append('<input id="street" name="street[{{$max}}]" class="form-control" type="text" placeholder="" value="">');
}
})

我不想在输入的 html 代码中使用 $max,而是使用 max js 变量的值,这样我就可以获得 street[2]

我见过很多关于如何只将变量传递给 append() 而不是 HTML 代码中的变量的解决方案...

最佳答案

$(wrapper).append('<input id="street" name="street[' + max + ']" class="form-control" type="text" placeholder="" value="">');

或者

$(wrapper).append(concat('<input id="street" name="street[', max, ']" class="form-control" type="text" placeholder="" value="">'));

关于javascript - 在 append() 中将 Javascript 变量传递给 HTML 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58151221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com