javascript - 在 append() 中将 Javascript 变量传递给 HTML 代码
问题描述
我想将一个变量传递给我要附加的 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="">'));
推荐阅读
- awk - AWK 无法对浮点数求和
- swift - 杀死并重新运行后台计时器(DispatchSourceTimer)
- docker - docker 容器时区未传播
- html - 如何避免 Flexbox CSS 中的水平和垂直滚动条?
- python - Tensorflow:从图像中预测浮点值总是返回0
- xml - XSLT 初学者:基于唯一元素值 XSLT 1.0 对 XML 进行分组
- python - 有什么方法可以让我的代码更容易理解和更简单?
- windows - 通过 USB NFC 设备将文件从 Windows 发送到支持 NFC 的手机
- php - 在mysql数据库中插入随机数取决于php中的某些列
- bash - 使用 Mac OSX 的 .bashrc 中的别名:命令在命令行上工作,但在别名中它不起作用