jQuery on button click not working

回眸只為那壹抹淺笑 提交于 2019-12-05 23:40:08

问题


I am using jquery mobile click function, however, it is not working.

Here is an example of the button that I have, and it is contained within a grid:

<div class="ui-block-c"><a class="request" data-role="button" data-id="\"'+json[i].num+'\" data-type="3" data-icon="plus" data-iconpos="right">Test</a></div>

jQuery function:

$('.request').on('click', function() {
    alert("hi");
});

How do I fix this?


回答1:


It looks like you are adding this element dynamically, so you'll need to use a delegated event listener:

$(document).on('click', '.request', function() {
    alert("hi");
});

Also you have an issue with your escaped quotes not matching. I don't think those are necessary:

<div class="ui-block-c"><a class="request" data-role="button" data-id="'+json[i].num+'" data-type="3" data-icon="plus" data-iconpos="right">Test</a></div>



回答2:


$(.request).click(function(){
alert("hi")
});


来源:https://stackoverflow.com/questions/18664567/jquery-on-button-click-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!