将带有字母字符的字符串参数传递给无法工作的javascript函数



我试图用由所有字母字符组成的参数字符串参数调用按钮的onclick事件的javascript函数(例如:test_function("1"、"10"、"fsdadfa");)但没能成功。但当我将字母字符改为所有数字字符时(例如:test_function("2"、"14"、"12234");)它奏效了。

msg += '<input type="submit" name="button" id="button" class="okbtn" value="OK" onclick="test_function('+id+','+user_id+','' +code+ '');"/>';
var phtml3 = '<div style="display: none;"><div id="share" style="overflow:auto;">'+msg+'</div></div>'; 
$('#my_div').append(phtml3);

有人知道为什么会发生这种事吗?如果是,请帮帮我。

看起来很草率,与其使用onclick,为什么不在该元素上附加一个事件侦听器呢?

<script>
var button = document.getElementById('button');
button.addEventListener('click', function() {
   test_function(id, user_id, code);
});
</script>

省去了处理转义字符串和在html页面中更难找到代码的痛苦。

最新更新