<td><input name="SeeUsers" id="SeeUsers" class="SeeUsersButton" value="See Users" type="button" title="#qPages.id#" onclick="ListUsers();"></td>
<script type="text/javascript">
ListUsers=function(){
var userid = $(this).title;
$('.userslistdiv').text(userid);
$('.userslistdiv').show;
};
</script>
我一直在尝试将此输入绑定(bind)到 jquery click 事件,但无法触发它。所以,我放弃了 jquery click 函数,只使用 onclick= 。没有人触发该事件。
问题可能是主页有一个 cfdiv 动态加载具有 onclick= 输入的内容。但我使用 jquery datepicker 在几个页面上执行此操作没有问题。
在开发工具中我可以看到脚本,它位于头部。
编辑后的代码:
ListUsers=function(el){
var userid = el.title;
$('.userslistdiv').text(userid);
$('.userslistdiv').show;
};
<input name="SeeUsers" id="SeeUsers" class="SeeUsersButton" value="See Users" type="button" title="#qPages.id#" onclick="ListUsers(this);"></td>
最佳答案
如果您尝试在动态添加的元素上触发事件,则必须首先选择包含动态添加的元素的已存在的元素。这可能是您已在其中附加了新元素的 div,或者如果您事先不知道该元素将添加到何处,则可以使用文档对象。
需要Javascript:(添加警报以让您知道该事件有效)
代码笔示例:http://codepen.io/larryjoelane/pen/zrEWvL
/* You can replace the document object in the parentheses
below with the id or class name of a div or container
that you have appended the td tag
*/
$(document).on("click","#SeeUsers",function(){//begin on click event
var userid = $(this).title;
$('.userslistdiv').text(userid);
$('.userslistdiv').show;
//test to see if event fired!
alert("it worked");
});//end on click event
关于javascript - jquery 点击不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27517419/