我从前几天开始在谷歌上搜索,但我从未发现我的代码有什么问题。 我的附加js代码是
$('.dynamic-form').append("<div class='row " + field_type + "-field" + field_count + "'>" +
"<button type='button' class='btn btn-primary' onClick='FieldEdit("+field_count + "," \''+field_type+ '\' + ")'>" + ' + "</button>");
Js函数是
function FieldEdit(id,type) {
$('#' + type + '-field' + id).toggle();
}
如果有人解决这个问题,我感到非常高兴和感激。 谢谢你!!!!
最佳答案
您需要为第二个参数添加正确的引号和转义符:
... "<button type='button' class='btn btn-primary'
onClick='FieldEdit(" + field_count + ",\"" + field_type + "\")'></button>");
控制台输出:
<button type='button' class='btn btn-primary'
onClick='FieldEdit(1,"This string")'></button>
关于javascript - 如何在onclick按钮传递函数的js函数中传递字符串第二个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44627605/