简体   繁体   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>

I've been trying to bind this input to a jquery click event but couldn't get it to fire. 我一直在尝试将此输入绑定到jquery click事件,但无法使其触发。 So, I dumped the jquery click function and just used onclick=. 所以,我转储了jquery点击功能,只使用了onclick =。 Neither one fires the event. 两人都没有开火。

The problem may be that the main page has a cfdiv that dynamically loads content that has the input with the onclick=. 问题可能是主页面有一个cfdiv,它动态加载具有onclick =输入的内容。 But I do this on several pages using jquery datepicker without a problem. 但我使用jquery datepicker在几个页面上执行此操作没有问题。

In dev tools I can see the script and it is in the head. 在开发工具中,我可以看到脚本,并且该脚本位于头部。

Edited code: 编辑代码:

    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>

If you are trying to fire an event on a dynamically added element you have to first select an element that already existed that encloses the dynamically added element. 如果要在动态添加的元素上触发事件,则必须首先选择一个已经包含动态添加的元素的元素。 This could be a div in which you have appended the new element or you can use the document object if you don't know ahead of time where the element will be added. 这可能是一个div,其中已添加了新元素,或者如果您不知道提前将元素添加到何处,则可以使用document对象。

Javascript needed:(alert added to let you know the event works) 需要Javascript :(添加警报以告知您事件的工作原理)

Code Pen example: http://codepen.io/larryjoelane/pen/zrEWvL 代码笔示例: 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

this inside the ListUsers does not refer to the clicked element so you need to pass the clicked element reference as a param ListUsers中的this ListUsers并不引用被单击的元素,因此您需要将单击的元素引用作为参数传递

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

then 然后

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

-- -

But since you have jQuery, use jQuery to handle events 但是由于你有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