2

jQuery is throwing the following error:

Uncaught TypeError: Object # has no method 'apply'

f.event.dispatch

h.handle.i

I've found these related posts, but couldn't solve the problem using them: This, this and this.

Here's the troublesome code:

$(document).ready(function(){
$('form.rating').click({
    cancelShow:true,
    callback: function(ui, type, new_value) {
        var values = ui.$form.serializeArray();
        values.push({
            'name': 'rating',
            'value': new_value
        });
        values = jQuery.param(values);
        var msg = ui.$form.attr('update-msg');
        $(msg).removeClass('hidden');
        $(msg).show();
        $(msg).find('div.msg-default').show();
        $(msg).find('div.msg-result').hide();
        $.ajax({
            'type': 'POST',
            'dataType': 'json',
            'url': ui.$form.attr('action'),
            'data': values
        }).done(function(data) { 
            var overall_rating = ui.$form.attr('update-overall');
            if(overall_rating && data['overall_rating']){
                $(overall_rating).html(data['overall_rating']);
            }
            if(msg){
                $(msg).find('div.msg-default').hide();
                if(data['msg']) {
                    $(msg).find('div.msg-result').show();
                    $(msg).find('div.msg-result').html(data['msg']);
                    window.setTimeout(function() {
                        $(msg).addClass('hidden');
                    }, 2000);
                } else {
                    $(msg).addClass('hidden');
                }
            }
            if(data['user_rating'] && data['user_rating']>0) {
                ui.select(parseInt(data['user_rating']));
            }
        });
    }
});
});

Any ideas?

Edit: Okay, so I stripped it down to:

$(document).ready(function(){
    $('form.rating').click({
    });
});

And it is still showing the same error. Could this have something to do with my jQuery script? The only other info it shows is this line, but I don't think it helps much, since the file has 3 lines in total: jquery-1.7.2.min.js:3


The jquery click handler expects an anonymous function, like this:

$(document).ready(function(){
    $('form.rating').click(function() {
        // go for it!
    });
});
4

2 回答 2

5

jquery click 处理程序需要一个匿名函数,如下所示:

$(document).ready(function(){
    $('form.rating').click(function() {
        // go for it!
    });
});
于 2013-03-20T14:38:06.877 回答
2

@Ludder is close, but off by just a little. He's right that you need to be passing a function as the argument to click, but it doesn't have to be anonymous.

a = {myFun: function(){console.log("a");}}
$(document).ready(function(){
    $('body').click(a.myFun);
});

and

b = function(){console.log("b");}
$(document).ready(function(){
    $('body').click(b);
});

and

function c () {console.log("c");}
$(document).ready(function(){
    $('body').click(c);
});

All log their bit of the alphabet. You were passing an empty object to click, give it a function instead.

于 2013-05-27T18:54:01.020 回答