如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
jq 获取数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
$(function(){ //点击之后获取内容 $('button').click(function(){ //方法一load $('#box').load('./1.php'); //方法二get $.get('./1.php',function(date){ $('#box').append(date); }) //方法三 post $.post('./1.php',function(data){ $('#box').append(data) }); //方法四ajax $.ajax({ type:'get', url:'./1.php', success:function(date){ alert(date); } }); }) }) |
带参数
1 2 3 4 5 |
<form id="myform"> 加数1 <input type="text" id="num1" name="a"><br> 加数2 <input type="text" id="num2" name="b"><br> </form> <div id="box"> |
2.php
1 |
echo $_POST['a'] + $_POST['b']; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
$(function(){ $('button').click(function () { var num1 = $('#num1').val(); var num2 = $('#num2').val(); //get方式 (路径,参数,function) $.get('./2.php',{a:num1,b:num2},function(data){ alert(data) }); //post方式 $.post('./2.php',{a:num1,b:num2},function(data){ alert(data) }); //ajax //另一种参数方式,get,post一样适用 //var cs = $('#myform input').serializeArray();//返回json对象 //var cs = $('#myform input').serialize();//返回字符串a=num1&b=num2 $.ajax({ type:'post', url:'./2.php', data:cs, success:function(data){ alert(data); } }) |
点击加载
1 2 3 4 5 6 7 8 9 |
<?php require './config.php'; require './Model.class.php'; $res = new Model('shop_user'); $result = $res->field(array('id','name','tel','email'))->order('id desc')->select(); echo json_encode($result);//json方式 字符串 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<script> $(function () { $('button').click(function () { $.ajax({ type:'get', url:'./3.php', success:function (data) { for (var i = 0; i< data.length; i++){ $('<li>').html(data[i].name).appendTo('#box'); } }, dataType:'json'//字符串转换为对象 }) }) }) </script> |
跨域
1 2 3 |
function loadHtml(){ $.getScript('http://localhost/1108/JQuery-03/6.php'); } |
6.php
1 2 3 4 5 6 7 8 9 |
<?php require './config.php'; require './Model.class.php'; $res = new Model('shop_user'); $result = $res->field(array('id','name','tel','email'))->order('id desc')->select(); echo "make(".json_encode($result).")"; |