有问题变成javascript生成的输入

时间:2014-10-20 20:02:55

标签: javascript escaping

所以我尝试使用Javascript生成输入表单,以便我可以添加多行相同的输入框。

var color_form = document.getElementById('color_form');
var create = document.createElement('input');
create.setAttribute('method', 'post');
create.setAttribute('name', "\"red[" + i + "]");
create.setAttribute('class', 'color_entry');
create.setAttribute('placeholder', 'red');
color_form.appendChild(create); //Append red value to color_form

我已尝试添加另一个结尾引号并将其关闭但有时它会完全省略该表单,有时它会给我这个

<input class="color_entry" method="post" name=""red[" + i + "]"" placeholder="red"></input>

有人能指出我在这里做错了吗?

2 个答案:

答案 0 :(得分:0)

create.setAttribute('name', "\"red[" + i + "]");

应该是

create.setAttribute('name', "red[" + i + "]");

无需逃避。

答案 1 :(得分:0)

不确定为什么要在name属性中转义引号:

create.setAttribute('name', "red[" + i + "]");
相关问题