我花了几个小时试图解决这个问题,并审查其他类似的StackOverflow问题(1)(2),但没有似乎有回答我的问题..的jQuery + Rails的:如何获得按钮用户的ID点击
我无法通过此错误:ReferenceError: id is not defined
。警报不显示。
$("button.btn-tag-cat").click(function(e){
e.preventDefault();
var id = $(this).prop('id');
alert(id);
id.find('span').show();
}, function(){
id.find('span').hide();
});
我试过以下方法来使用id变量但没有工作。
$(id).find('span').show();
$("#" + id).find('span').show();
然而,当我删除的警报下面的代码块,警报会弹出属于按钮正确的ID。
$("button.btn-tag-cat").click(function(e){
e.preventDefault();
var id = $(this).prop('id');
alert(id);
});
查看部分: 按钮的ID引用红宝石局部变量id="<%= name %>"
<% q.categories_name_in.each do |name| %>
<button type="button" class="btn btn-outline-primary btn-lg btn-tag-cat" id="<%= name %>"><%= name %><span class='glyphicon glyphicon-remove'></span></button>
<% end %>
感谢明确的答复,但是,我现在收到此错误:类型错误:id.find不是函数 – doyz
使用'$(id)'代替id。 –
该ID是一个字符串。使用'$('#'+ id).find(...)' –