我试图在我的 javascript 函数中将一个 int 连接到我的 id,但不知何故 id 不起作用,我得到一个 NaN。
代码:
observeBox: function() {
$('#selection').on('click', function() {
$selection = $(this).val();
$selection.forEach(function(currentValue,index,array){
console.log(index);
$('#sliderAppend').append(
+'<div id=\"showInput'+index+'_'+$selection[index]+'\">'
+'<span class=\"btn btn-primary btn-file\">'
+'<span class=\"fileupload-new\">'+$selection[index]+'</span>'
+'<input id=\"subtitles_file\" name=\"subtitles_file\" type=\"file\" class="\ImageInput upload\" value=\"\">'
+'</span>'
+'<span class=\"fileupload-preview\"></span>'
+'</div>');
});
console.log($selection);
});
},
基本上我在这一行中收到错误:
+'<div id=\"showInput'+index+'_'+$selection[index]+'\">'
不知何故它无法正确连接索引。
如果我
console.log(index);
我得到了正确的值。
$selection[index]
也返回正确的值。
最佳答案
删除第一个 +
来自 +'<div id=\"showInput'+index+'_'+$selection[index]+'\">'
的字符
应该是:
'<div id=\"showInput'+index+'_'+$selection[index]+'\">'
拥有领先+
尝试将其后面的字符串转换为数字,但失败并给出 NaN
.
关于Javascript Concat int 到 html id NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38657530/