PHP+Jquery点击提交

有时我们不希望用户点击提交来请求服务器数据, 利用Jquery可以实现。

test.html

<!DOCTYPE html> 
<html> 
<body>
<div id="select"> 
  <input name="show" type="radio" value="all" /> <label for="all"> All</label> 
  <input name="show" type="radio" value="rank" /> <label for="rank"> Top10</label> 
</div>
<div> 
  <form action="data.html" method="post">
<textarea id="res" name="res" row="4"></textarea><br /> 
  <input type="submit" value="Submit" /> 
  </form>
</div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<script src="submit.js"></script> 
</body>
</html> 

submit.js

$(function () {
    //alert('works!');
    var url = 'http://localhost:8080/test/data.php';
    var input=$('input[type="radio"]');
    var res=$('#res');
    input.click(function () {
        $(this).attr('checked');
        var type = $(this).val();
        console.log(type);
        $.ajax({
            url: url,
            data: {'type':  type },
            dataType: 'json'
        })
        .success(function( data ){
            res.html( data );
            console.log( data );
        });
    });
});

data.php

<?php
if (isset($_GET['type'] )){
   echo json_encode([$_GET['type'] , ' Method']);
}else{
   echo 'ERROR';
}
?>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 文/顾修 我听说 你在花开的四月里 漫步清池 ...
    顾修阅读 249评论 2赞 2
  • 有些人会一直刻在记忆里,即使忘记了她的声音,忘记了她的笑容忘记了她的脸。但是每当想起她时的那种感受,是永...
    xiao炜阅读 580评论 0赞 0
  • 第一次见你 是在陌生的微信 并不想动情 动情是意料不及 你说你无情 事实并不是如此 你对你兄弟无论快乐苦难都不离 ...
    欣儿____阅读 800评论 0赞 0

友情链接更多精彩内容