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

    评论

报告相同问题?