繁体   English   中英

jquery点击不解雇

[英]jquery click not firing

    <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>

我一直在尝试将此输入绑定到jquery click事件,但无法使其触发。 所以,我转储了jquery点击功能,只使用了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,其中已添加了新元素,或者如果您不知道提前将元素添加到何处,则可以使用document对象。

需要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

ListUsers中的this ListUsers并不引用被单击的元素,因此您需要将单击的元素引用作为参数传递

<input name="SeeUsers" id="SeeUsers" class="SeeUsersButton" value="See Users" type="button" title="#qPages.id#" onclick="ListUsers(this);">

然后

ListUsers = function(el) {
    var userid = el.title;
    $('.userslistdiv').text(userid);
    $('.userslistdiv').show();
};

-

但是由于你有jQuery,使用jQuery来处理事件

jQuery(function ($) {
    $('#SeeUsers').click(function () {
        var userid = this.title;
        $('.userslistdiv').text(userid);
        $('.userslistdiv').show(); //it is a function
    });
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM