Onclick不起作用 [英] Onclick is not working
问题描述
大家好,这是Nabeel我是jquery和json的新手
Hi guys this is Nabeel I'm new in jquery and 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,以便我可以在任何地方展示
I want to give every recipe its different id so that i can show it 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但当我按下视图但它什么都不做时
Its my ViewItemFunction but when i press on view but it do 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()
:
$(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-part的缺席。
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屋!