weixin_33675507 2014-05-14 13:03 采纳率: 0%
浏览 148

jQuery addEventListener + Ajax

I have a problem, my jQuery function doesn't work. It's initialize (alert before addEventListener works, but the click is not detected. The tricks works with a

onclick="attachEventsFid2()"

But not with my code :

function attachEventsFid2() {
    var main=$('.main-wrapper');
    var btn = document.getElementById("btn-iphone4");

    btn.addEventListener('click', function(event) {
        event.preventDefault();
        $.ajax({
            type:"post",
            url:BASE_URL+'index/formfid2/',
            data:{'iphone':iphone},
            cache:false,
            success:function(data){
            main.parent().html(data);    
            }
        });
    });
};

Html part :

<div class="row row3">
<a id="btn-iphone5" class="ico-reserver choix-iphone"  type="button" value="iphone5"  ></a>
</div>

Logs :

Uncaught TypeError: Cannot read property 'addEventListener' of null 

With getElementsByClassName i have an other log :

Uncaught TypeError: undefined is not a function (=>
btn.addEventListener('click', function(event) { ... )

Thanks !

  • 写回答

3条回答 默认 最新

  • weixin_33735077 2014-05-14 13:08
    关注
    btn.on('click', function(event) {
        event.preventDefault();
        $.ajax({
            type:"post",
            url:BASE_URL+'index/formfid2/',
            data:{'iphone':iphone},
            cache:false,
            success:function(data){
            main.parent().html(data);    
            }
        });
    });
    

    Try that, just use "on" ;)

    If your alert works before that should be great

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题