Using JQuery AJAX to grab some data from the Database and print them out according to each DIV ID
I was trying to grab some data from the database using JQuery AJAx, and print the data to the DIV with a unique ID.
I got two DIV tags like this:
<div id="1" class="myClass">
The content for texting1 is : $printing result from the Test Table which id=1
</div>
<div id="2" class="myClass">
The content for texting2 is : $printing result from the Test Table which id=2
</div>
Here is the function in a Contr开发者_JAVA百科oller:
function findcontent($id=null){
$this->autoRender=false;
$result=$this->Test->findByid($id);
if($result){
return $result['content'];
}
else{
return "nothing";
}
}
<script type="text/javascript">
$(document).ready(function(){
var id=$("div #myClass").val(); // I am stuck over this line of code
var curl="http://localhost:8080/test/findcontent"+id;
$.ajax({
type: "POST",
url: curl,
success: function(data) {
var w=data.substring(0,1);
alert(data);
});
});
});
</script>
I have no idea how to move on since I there are more than two DIV tags in my case.
I need your help.Try this
$.ajax({
type: "POST",
url: curl,
success: function(data) {
$("#1").html(data);
$("#2").html(data);
});
});
});
Do you want to add same data in both div?
精彩评论