JSON
找元素:
1.根据ID找 $("#id")
2.根据class找 $(".class")
3.根据属性找 $("[name=value]")
4.根据标签名找 $("div")
操作内容
表单
$("#id").val();
非表单
$("#id").text();
$("#id").html();
操作属性
$("#id").attr();
$("#id").removeAttr();
操作样式
$("#id").css();
事件:
1.不写on
2.不在标签里面写
3.用jquery代码给元素绑定事件
$("#id").change(function(){
})
$("#id").bind("change",function(){
})
</div>
<script type="text/javascript">
//JSON类型数据
//定义:花括号括起来,key和value成对存在,可以存储任何类型数据
var js = {
"one":"1111",
"two":"2222",
"three":new Array(1,2,3),
"four":{a:1,b:2,c:3}
};
var cc = {4:12345};
js.one.push(cc);
//循环JSON数据
for(var k in js)
{
alert(js[k]);
}
//将数组转换为JSON
echo json_encode($arr);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="popwindow/jquery-1.11.2.min.js"></script> </head> <body> <br /> <div>请输入代号:<input type="text" id="code" /> <input type="button" value="查询" id="btn" /> </div> <br /> <div id="name"></div> <br /> <br /> <div>请选择:<select id="sel"> <option value="p001">张三</option> <option value="p002">李四</option> <option value="p003">王五</option> </select></div> <br /> <div id="xinxi"></div> </body> <script type="text/javascript"> $(document).ready(function(e) { $("#btn").click(function(){ var code = $("#code").val(); $.ajax({ url:"selchuli.php", data:{code:code}, type:"POST", dataType:"JSON", success: function(data){ $("#name").text(data.name); } }); }) $("#sel").change(function(){ var code = $(this).val(); $.ajax({ url:"xxchuli.php", data:{code:code}, type:"POST", dataType:"JSON", success: function(data){ var str = "<span>代号:"+data[0]+"姓名:"+data[1]+"性别:"+data[2]+"生日:"+data[4]+"</span>"; $("#xinxi").html(str); } }); }) }); </script> </html>
处理页面
<?php $code = $_POST["code"]; include("../DBDA.php"); $db = new DBDA(); $sql = "select Name from Info where Code = '{$code}'"; $attr = $db->Query($sql); //做一个关联数组 $arr = array(); $arr["name"] = $attr[0][0]; //将数组转换为JSON echo json_encode($arr);
ajax实现返回json
<script type="text/javascript"> $(document).ready(function(e) { $.ajax({ url:"jsonchuli.php", dataType:"JSON", success: function(data){ for(var k in data) { alert(data[k]); } } }); }); </script>