这是我的代码:
$(document).on("click",'#comment-post-btn', function() {alert("yes"); });
此处,ID #comment-post-btn
需要替换为最终将解析为#comment-post-btn
的变量
我该怎么做?
修改
我已将id名称分配给javascript变量var id_name = "#comment-post-btn"
。现在我想用这个变量代替
$(document).on("click",'<idname>', function() {alert("yes"); });
有些事情,
$(document).on("click",'id_name', function() {alert("yes"); });
答案 0 :(得分:3)
var btn_id = 'comment-post-btn';
$(document).on("click",'#' + btn_id , function() {alert("yes"); });
// ^ string concatenation
<小时/> 如果您已为{id>选择
添加了
#
var btn_id = '#comment-post-btn';
$(document).on("click", btn_id , function() {alert("yes"); });
答案 1 :(得分:0)
var _ButtonID = 1;
$(document).on("click","#Button" +_ButtonID , function() {alert("yes"); });
将导致
$(document).on("click","#Button1" , function() {alert("yes"); });