jquery - How can i change image after get data from database -
for function getdatafromdb()
newest data database , want use val["id"]
to define condition change image see on function getimage
it's not success. can give me example ?
<script> function getdatafromdb() { $.ajax({ url: "getdata.php" , type: "post", data: '' }) .success(function(result) { var obj = jquery.parsejson(result); if(obj != '') { //$("#mytable tbody tr:not(:first-child)").remove(); $("#mybody").empty(); $.each(obj, function(key, val) { var tr = "<tr>"; tr = tr + "<td>" + val["id"] + "</td>"; tr = tr + "<td>" + val["first_name"] + "</td>"; tr = tr + "<td>" + val["last_name"] + "</td>"; tr = tr + "<td>" + val["age"] + "</td>"; tr = tr + "<td>" + val["hometown"] + "</td>"; tr = tr + "<td>" + val["job"] + "</td>"; // tr = tr + "<td>" + "<img src='" + getimage(parseint(val["id"]))+"'>" + "</td>"; tr = tr + "</tr>"; $('#mytable > tbody:last').append(tr); }); } }); } function getimage(id){ var image = document.getelementbyid("myimage"); if (image.val["id"] < 10){ return "pic/img1.jpg" } else if (image.val["id"] < 20){ return "pic/img2.jpg" } else { return "pic/img3.jpg" }; } setinterval(getdatafromdb, 100); // 1000 = 1 second </script>
uncomment commented tr
in each
loop , use following function
function getimage(id){ if (id< 10){ return "pic/img1.jpg" } else if (id< 20){ return "pic/img2.jpg" } else { return "pic/img3.jpg" }; }
Comments
Post a Comment