Onclick无效

大家好,这是Nabeel我是jquery和json的新手

我的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,以便我可以在任何地方展示它

  $('#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='
'; $(content).appendTo("#recipes"); }); }); });

它是我的ViewItemFunction,但当我按下视图但它什么也不做

  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()任何位置添加它:

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

改变这个:

 content+='
  • 对此:

     content+='
  • 注意“btn”之后的“ - ”和没有“onclick” - 部分。

    我认为问题存在是因为您动态添加输入,请参见此处 。 如果这没有帮助它可能是一个语法错误,你的代码是非常结构化的,并说实话我懒得经历这一切。

    如果您使用谷歌浏览器,您可以按F12并单击“控制台”以查看是否有错误。