Jquery AJAX
做毕设时候写过很多的ajax,抽出一小段发上来,以便回忆。
1 $.ajax({ 2 url: "/Menus/GetMenus?id="+value, 3 type: "post", 4 success: function (result) { 5 var data = eval('{' + result + '}'); 6 var divstr = ""; 7 for (var i = 0; i < data.length; i++) { 8 divstr += "<div style=\"float:left;margin-left:5px;margin-top:5px;width:170px;height:240px;border:1px solid #9D9D9D;\" id=" + data[i].id + ">"; 9 divstr += "<div style=\"float:left;margin-left:5px;margin-top:5px;width:160px;height:160px;background-image:url('../image/" + data[i].name + ".jpg');\"></div>"; 10 divstr += "<div style=\"float:left;width:160px;height:70px;margin-left:5px;background-color:#F7F6F6\">"; 11 divstr += "<div style=\"float:left;width:160px;height:15px;margin-left:5px;font-size:11px;font-weight:800;line-height:15px;\">" + data[i].name + "</div>"; 12 divstr += "<div style=\"float:left;width:160px;height:25px;margin-left:5px;font-size:11px;line-height:25px;\">"; 13 divstr += "<div style=\"float:left;width:60px;height:25px;\">" + data[i].price + "元/份</div>"; 14 divstr += "<div style=\"float:left;width:90px;height:25px;\">"; 15 //下面是加减号 16 divstr += "<div style=\"float:left;width:22px;height:22px;margin-top:1px;background-image:url('../image/减号.gif');cursor:pointer;\" onclick=\"Jianfa('caidan'+'"+data[i].id+"')\"></div>"; 17 divstr += "<input id=\"caidan"+data[i].id+"\" style=\"float:left;width:34px;height:16px;margin-top:1px;margin-left:2px;\" type=\"text\" value = \"1\"/>"; 18 divstr += "<div style=\"float:left;width:22px;height:22px;margin-top:1px;margin-left:2px;background-image:url('../image/加号.gif');cursor:pointer;\" onclick=\"Jiafa('caidan'+'" + data[i].id + "')\"></div>"; 19 divstr += "</div>"; 20 divstr += "</div>"; 21 //下面是订购按钮 22 divstr += "<div style=\"float:left;width:70px;height:20px;margin-left:40px;margin-top:5px;background-image:url('../image/订购.jpg');cursor:pointer;\" onclick=\"Dinggou('" + data[i].id + "','" + data[i].name + "','" + data[i].price + "','caidan'+'" + data[i].id + "')\"></div>"; 23 divstr += "</div>"; 24 divstr += "</div>"; 25 } 26 $('#middle').html(divstr); 27 }, 28 error: function () { } 29 });
主要还是在于语法和结构。理解了这些才能越写越好。