Onclick 事件处理程序似乎没有被调用 [英] Onclick event handler seems not called

查看:30
本文介绍了Onclick 事件处理程序似乎没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 JSON:

[{
"i_id":"1",
"i_name":"Biryani",
"i_category":"Pakistani",
"i_Uploader_Name":"Nabeel",
"i_dateofadd":"2016-01-24",
"i_rate":"0",
"image_name":"Biryani-main1.jpg",
"image_path":"images\/Biryani-main1.jpg",
"image_thumb":"thumb\/Biryani-main1.jpg"
}]

我想为每个食谱赋予不同的 ID,以便我可以在任何地方展示它们:

I want to give to every recipe its different id, so that I can show them anywhere:

 $('#btnsearchres').click(function(){
            $("#recipes").empty();
            var content;
            var ingredient=$('#txtsearch').val();
            //$server = 'http://localhost/XDK';     
 $.getJSON(server+"/search.php",{ingredient:ingredient},function(data){
    $.each(data, function(i,data){
    content='<ul data-role="listview" data-inset="false" data-theme="a">';
    content += '<li><a>' + data.i_category + '</a></li>';
    content += '<li><a href="#"><img src="http://localhost/xdk/'+data.image_thumb+'" style="width:200px;height:200px;"/>';
        content+='<h3>'+data.i_name+'</h3>';
    content += '<p overflow: hidden; text-overflow: ellipsis;white-space: nowrap;">' + data.i_recipe + '</p></a></li>';
        content+='<li><input id="btn'+data.i_id+'" onclick="ViewItem('+data.i_id+','+UserId+')" type="button" value="View" style="padding-top:10px;"/></li>';
        content+='</ul><hr>';
    $(content).appendTo("#recipes");
    });
});
});

这是我的 ViewItemFunction.但是当我按下视图时,它什么也没做:

It is my ViewItemFunction. But when I press on view, it does nothing:

 function ViewItem(ItemId,UserId){
    alert("working");//not working
    var content;
    $.ajax({

                           type: "post",
                           dataType  : 'html',
                           url: $server+"/view.php",
                           data: {ItemId:ItemId,UserId:UserId},
                           success: function(result) {
                               alert(result);
                               
                                $(content).appendTo("#");
                            }
                       });
}

推荐答案

在你的 $(document).ready() 的任何地方添加:

Add this anywhere in your $(document).ready():

$(document).on('click', '#btn', function() {
    ViewItem($(this).attr('id').substr($(this).attr('id').indexOf('-') + 1), UserId);
}

改变这个:

content+='<li><input id="btn'+data.i_id+'" onclick="ViewItem('+data.i_id+','+UserId+')"

为此:

content+='<li><input id="btn-'+data.i_id+'"

注意btn"后面的-"和onclick"部分的缺失.

Note the "-" after "btn" and the absence of the "onclick"-part.

我认为问题存在是因为您动态添加输入,请参阅这里.如果这没有帮助,则可能是语法错误,您的代码结构非常糟糕,说实话我懒得全部看完.

I think the problem exists because you add the input dynamically, see here. If this does not help it might be a syntax error, your code is terribly structured and to say the truth I am too lazy to go through it all.

如果您使用谷歌浏览器,您可以按 F12 并点击控制台"查看是否有错误.

If you use Google Chrome you can press F12 and Click on "Console" to see if there is an error.

这篇关于Onclick 事件处理程序似乎没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆