我正在尝试将 2 个数字解析为函数 saveQuestion
函数的参数,但它不起作用。
$("#questionRow")
.append('<input type="submit" class="btn btn-default" id="saveQuestion'
+ addSectionCount + '" value="Save question '
+ addSectionCount + '" onclick="saveQuestion('
+ addOpSectionCount + '\',\'' addSectionCount + ')" ></div>');
最佳答案
如果您只是使用 jQuery 来注册一个适当的回调函数而不是使用 90 年代风格的内联事件处理程序,那么它会不那么脆弱并完全避免这个问题:
$('<input type="submit" />')
.attr('id', 'saveQuestion' + addSectionCount)
.val('Save question ' + addSectionCount)
.on('click', function() {
saveQuestion(addOpSectionCount, addSectionCount);
})
.appendTo('#questionRow')'
另请注意 jQuery 函数的其他用途,以避免在 HTML 字符串中使用字符串连接。
注意:我已经反转了 append
这样链式.on
调用适用于新创建的元素,不适用于 #questionRow
.
注意 2:我通常会使用 $('<el>', {id: ..., val: ...})
但是 MSIE 的 ISTR(曾经?)输入元素的语法存在问题。
关于JavaScript - 在函数内连接 2 个参数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36084840/