Jquery 动态按钮并打开。单击事件问题

Jquery 动态按钮并打开。单击事件问题,jquery,jsonp,Jquery,Jsonp,我正在生成具有唯一id的动态css按钮,并使用以下单击处理程序捕获单击事件 $(document.body).on('click', 'a', function() { if (typeof(this.id) != "undefined") { //check if id exists var n = this.id.split("%"); if (n[0] == "jsonpbtn") { //check if one of our buttons

我正在生成具有唯一id的动态css按钮,并使用以下单击处理程序捕获单击事件

$(document.body).on('click', 'a', function() {
    if (typeof(this.id) != "undefined") { //check if id exists
        var n = this.id.split("%");
        if (n[0] == "jsonpbtn") { //check if one of our buttons    
            var surl = "http://www.blah.com/tree/jsonp/foo/" + n[1];
            $.ajax({
                url: surl,
                cache: false,
                dataType: "jsonp",
                jsonp: false,
                jsonpCallback: 'foo',
                error: function(XHR, textStatus, errorThrown) {
                    alert(textStatus + ":" + errorThrown)
                },
                success: function(foo) {
                    $('#blah' + foo.id).html(foo.message);
                }
            });
        }
    }
});​
虽然ajax调用可以工作,但页面会生成一个错误“parsererror:error:foo未被调用”;我相信问题是这个循环通过了页面上的所有“a”元素
为每个“jsonpbtn”调用函数?

我发现,通过在ajax调用之前插入一个初始阶段,在其中更改按钮状态,可以解决问题

$(document.body).on('click', 'a', function() {
if (typeof(this.id) != "undefined") { //check if id exists
    var n = this.id.split("%");
    if (n[0] == "jsonpbtn") { //check if one of our buttons    
        var surl = "http://www.blah.com/tree/jsonp/foo/" + n[1];
        $('#div_id'+n[1]).html('<a class="button" href="#">working</a>');//<-added this
        $.ajax({
            url: surl,
            cache: false,
            dataType: "jsonp",
            jsonp: false,
            jsonpCallback: 'foo',
            error: function(XHR, textStatus, errorThrown) {
                alert(textStatus + ":" + errorThrown)
            },
            success: function(foo) {
                $('#blah' + foo.id).html(foo.message);
            }
        });
    }
}
});​
$(document.body).on('click','a',function()){
if(typeof(this.id)!=“undefined”){//检查id是否存在
var n=此.id.split(“%”);
如果(n[0]==“jsonpbtn”){//检查我们的一个按钮
var surl=”http://www.blah.com/tree/jsonp/foo/“+n[1];

$('#div_id'+n[1]).html('')//问题是
http://www.blah.com/tree/jsonp/foo/n[1] 
没有返回名为
foo
的JSONP函数。响应应该类似于
foo({“foo”:“bar”})
同意Kevin B。它在服务器端,只是格式错误的JSONP,没有
foo时不会调用foo(JSON)
如前所述,ajax调用有效,如果页面上只有一个按钮,则不会出现错误。