Jquery,ajax返回json数据后呈现到html页面的$.post方式。
------------------------------------------------------完整版----------------------------------------------------------------------------
$(document).ready(function () {
$(".dazhao").click(function(){
$("#fade").hide(200);
$(".white_content").hide(200);
$("#anwser1").show(300);
var hol=$(this).attr("id");
$.post("data.php", {"types":hol},function (data) {
var index = 0;//声明一个出题目数量的初始变量
$("#anwser1 #question").html("<p>" + (index + 1) + "." + data[index]["question"] + "</p>");
$.each(data[index]["answers"], function (i, item) {
var xuanze1 = $("#anwser1 #chose .xuanze1").eq(i);
xuanze1.html(" " + xuanze1.attr("id") + "." + item);
});
var result=0; //声明一个判断用户答案对错的初始变量
$(".xuanze1").click(function () {
if($(this).attr("id")==data[index]["correct"]){
result=++result;
}
// alert(result);
index=index+1;
if(index<3){
$("#anwser1 #question").html("<p>" + (index + 1) + "." + data[index]["question"] + "</p>");
$.each(data[index]["answers"], function (i, item) {
var xuanze1 = $("#anwser1 #chose .xuanze1").eq(i);
xuanze1.html(" " + xuanze1.attr("id") + "." + item);
});
//alert("正确答案为"+data[index]["correct"]+" 即将进入下一题");
}else{
$("#anwser1").hide();
$("#fenxiang").show();
$("body").css("background-image","url(images/bg_share.jpg)");
//根据用户选择的测试类别进行判断输出
switch(hol){
case'ad':hol='广告策划师';
break;
case'web':hol='WEB前端工程师';
break;
case'produce':hol='产品专员';
break;
case'ui':hol='UI设计师';
break;
case'php':hol='PHP工程师';
break;
case'java':hol='Java工程师';
break;
case'and':hol='Android工程师';
break;
}
//根据答对题目的个数分配比例
switch(result){
case 0:result='28%';
break;
case 1:result='58%';
break;
case 2:result='78%';
break;
case 3:result='93%';
break;
}
$("#fenxiang #p111").html(result);
$("#fenxiang #type111").html(hol);
}
});
}, "JSON");
});
});
<div id="anwser1" style="display:none">
<div id="question">
<p id="p1"></p> </div>
<div id="chose">
<div id="A" class="xuanze1"></div>
<div id="B" class="xuanze1"></div>
<div id="C" class="xuanze1"></div>
</div>
</div>
$(document).ready(function () {
$(".dazhao").click(function(){
$("#fade").hide(200);
$(".white_content").hide(200);
$("#anwser1").show(300);
var hol=$(this).attr("id");
$.post("data.php", {"types":hol},function (data) {
var index = 0;
$("#anwser1 #question").html("<p>" + (index + 1) + "." + data[index]["question"] + "</p>");
$.each(data[index]["answers"], function (i, item) {
var xuanze1 = $("#anwser1 #chose .xuanze1").eq(i); //因为有3个xuanze1,eq(i)就是获取这个列表对象的第几个条目的对象了
xuanze1.html(" " + xuanze1.attr("id") + "." + item);
});
$(".xuanze1").click(function () {
// alert("正确答案为"+data[index]["correct"]+" 即将进入下一题");
index=index+1;
if(index<3){
$("#anwser1 #question").html("<p>" + (index + 1) + "." + data[index]["question"] + "</p>");
$.each(data[index]["answers"], function (i, item) {
var xuanze1 = $("#anwser1 #chose .xuanze1").eq(i);
xuanze1.html(" " + xuanze1.attr("id") + "." + item);
});
}else{
$("#anwser1").hide();
$("#fenxiang").show();
}
});
}, "JSON");
});
});
<?php
include_once("conn.php");
$types=$_POST['types'];
$sql="select distinct* from test01 where types='$types' order by rand() limit 0,3";
$query = mysql_query($sql,$conn);
while($row=mysql_fetch_array($query)){
$answers = explode('###',$row['answer']);
$arr[]= array(
'question' =>$row['question'],
'answers' => $answers,
'correct'=>$row['correct'],
);
}
$json=json_encode($arr);
echo $json;
?>

Jquery,ajax返回json数据后呈现到html页面的$.post方式。的更多相关文章
- jquery ajax返回json数据进行前后台交互实例
jquery ajax返回json数据进行前后台交互实例 利用jquery中的ajax提交数据然后由网站后台来根据我们提交的数据返回json格式的数据,下面我来演示一个实例. 先我们看演示代码 代码如 ...
- PHP AJAX 返回JSON 数据
例子:利用AJAX返回JSON数据,间接访问数据库,查出Nation 表,并用下拉列表显示 造一个外部下拉列表框 </select> JQurey代码 $(document).ready( ...
- Ajax前台返回JSON数据后再Controller中直接转换成类型使用,后台接收json转成实体的方法
之前写过一篇记录文章,写的是将一个比较复杂的数据结构在前台组合起来后传递到后台. 当时并不太了解@RequestBody,也并没有使用js提供的JSON.stringify()方法 所有都是自己写的, ...
- jquery实现ajax,返回json数据
jquery实现ajax可以调用几种方法 我经常用的是$get(url,data,callback,type)方法 其中url是异步请求的页面(可以是.ashx文件),data是参数,callback ...
- 如何在.NET MVC中使用jQuery并返回JSON数据
http://blog.csdn.net/dingxingmei/article/details/9092249 开始实践 - jQuery端 假设我们要从服务器端获取一个文章列表,并把文章条目显示在 ...
- [转]如何在.NET MVC中使用jQuery并返回JSON数据
本文转自:http://blog.sina.com.cn/s/blog_48e42dc90100xp1p.html 二.开始实践 - jQuery端 假设我们要从服务器端获取一个文章列表,并把文章条目 ...
- JQuery ajax返回JSON时的处理方式
最近在使用JQuery的ajax方法时,要求返回的数据为json数据,在处理的过程中遇到下面的几个问题,那就是采用不同的方式来生成json数据的时候,在$.ajax方法中应该是如何来处理的,下面依次来 ...
- 通过ajax获得json数据后格式的转换
在有些情况下获取到的json数据可能是string类型的,需要把其格式化为json对象才方便解析. a)原生js通过ajax获取到的json 此时返回的数据默认是string型的,所以需要用eval( ...
- ajax返回json数据示例
前端发送请求与接收数据: $.ajax({ type : "post", url : "/queryStudent", ...
随机推荐
- delphi数组作为参数传值
在函数中如果数组的个数不定,可以使用开放数组参数 实参可以接受静态数组和动态数组 procedure p1(a:array of Byte); begin ShowMessage( IntToHex( ...
- mousedos网络批量部署xp
小时候对这个东西很好奇,不知道什么原理.一直觉得很好玩.现在研究了下,总结如下 软件的操作步骤很讲究,稍微不慎,则就需要重新来过 知识点: 1,掌握诺顿ghost分区为gh文件 2,学会清理至一个干净 ...
- 802.1x协议&eap类型
EAP: 0,扩展认证协议 1,一个灵活的传输协议,用来承载任意的认证信息(不包括认证方式) 2,直接运行在数据链路层,如ppp或以太网 3,支持多种类型认证 注:EAP 客户端---服务器之间一个协 ...
- C语言 百炼成钢12
//题目34:对10个数进行排序 #include<stdio.h> #include<stdlib.h> //分析:使用冒泡排序 void main(){ ] = { , , ...
- PC网站应用接入微信登录
参考文档: https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&ve ...
- 在matlab中进行遥感影像地理坐标的相互转换
在matlab中进行图像处理,一般使用的都是图像本地坐标,以左上角(1,1)开始.处理完成后,如果要将结果在带地理坐标的遥感影像中显示,或者需要输出成shp文件,就需要涉及到本地坐标和地理坐标的转换, ...
- 路由器 NorFlash与NandFlash区别
在淘宝上买修改openwrt的路由器,基本上都是改的16MB flash,那么为什么不改到1GB呢? 现在U盘的价格也很便宜啊. 于是我调查了一下,发现flash分为两种,NorFlash与NandF ...
- 编写高质量代码--改善python程序的建议(六)
原文发表在我的博客主页,转载请注明出处! 建议二十八:区别对待可变对象和不可变对象 python中一切皆对象,每一个对象都有一个唯一的标识符(id()).类型(type())以及值,对象根据其值能否修 ...
- libtool: Version mismatch error 解决
在编译一个软件的时候,在 ./configure 和 make 之后可能会出现如下错误: libtool: Version mismatch error. This is libtool 2.4. ...
- php检测php.ini是否配制正确
运行命令行 php -d display_startup_errors=1 -d error_reporting=-1 -d display_errors -c "C:\path-to-ph ...