作畢設時候寫過不少的ajax,抽出一小段發上來,以便回憶。html
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 });
主要仍是在於語法和結構。理解了這些才能越寫越好。ajax