3
嗨,大家好,這是納比爾我在jQuery和JSON的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,這樣我可以在任何地方表現出來
$('#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但是當我按下看法,但它什麼都不做
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("#");
}
});
}
什麼是'UserId'? '[...] onclick =「ViewItem('+ data.i_id +','+'** UserId **'+')」type =「button」[...]'我覺得有義務告訴你[使用onClick是不好的做法](http://stackoverflow.com/questions/5871640/why-is-using-onclick-in-html-a-bad-practice) – WcPc
UserId是1在這種情況下 –
我可以使用其他比Onclick? –