JQuery的常用语法

来源:互联网 发布:合肥网络传销公司名单 编辑:程序博客网 时间:2024/05/17 21:46

一.jQuery.post(url, [data], [callback], [type])

1.概述:这是一个简单的 POST 请求功能以取代复杂 $.ajax 。请求成功时可调用回调函数。如果需要在出错时执行函数,请使用 $.ajax。

2.参数:url,[data],[callback],[type]     String,Map,Function,String

url:发送请求地址。

data:待发送 Key/value 参数。

callback:发送成功时回调函数。

type:返回内容格式,xml, html, script, json, text, _default。

3.示例:

$.post("test.php"); //请求 test.php 网页,忽略返回值

$.post("test.php", { name: "John", time: "2pm" } ); //使用 ajax 请求发普通数据,忽略返回值

$.post("test.php", { 'choices[]': ["Jon", "Susan"] }); //使用 ajax 请求发送数组,忽略返回值

$.post("test.php", $("#testform").serialize()); //使用 ajax 请求发送表单数据,忽略返回值

$.post("test.php", function(data){ alert("Data Loaded: " + data); });

$.post("test.php", { name: "John", time: "2pm" }, function(data){ alert("Data Loaded: " + data); });

$.post("test.php", { name: "John", time: "2pm" }, function(data){ process(data); }, "xml");

$.post("test.php", { "func": "getNameAndTime" }, function(data){ alert(data.name); console.log(data.time); }, "json");


二.jQuery.ajax(url,[settings])

1.概述:jQuery 底层 AJAX 实现。简单易用的高层实现见 $.get, $.post 等。$.ajax() 返回其创建的 XMLHttpRequest 对象。

2..示例:

var id = trs[0].find(":input[name='id']").val();
$.ajax({
            async: false,
            url : basePathConst+"/letv/gcr/distribution_contractAction/delete.action",
            data: {"id":id},
            type: 'post',
            dataType: 'json',
            cache: false,
            beforeSubmit: function(formData) {}, //在提交表单之前调用。
            beforeSend: function() {}, //在发送请求之前调用。
            complete: function() {}, //当请求完成之后调用这个函数,无论成功或失败。
            error: function(d) { //在请求出错时调用。
                alert(d.statusText);
            },
            success: function(d) { //当请求成功之后调用。
                alert(d.message);
                window.location.reload();
            }
});


三.each(callback)

1.概述:以每一个匹配的元素作为上下文来执行一个函数。返回 'false' 将停止循环 (就像在普通的循环中使用 'break')。返回 'true' 跳至下一个循环(就像在普通的循环中使用'continue')。
2.示例:

<script type="text/javascript">

        $(document).ready(function() {
             $("div[q-id]").each(function() {
                  var rs = $(this).attr("result-id").split(",");
                  for (var i = 0; i < rs.length; i++) {
                       if ($(this).attr("option-id") == rs[i]) {
                            $(this).toggleClass("cur"); //如果存在(不存在)就删除(添加)一个类。
                       }
                  }
             });
        });
</script>

<c:forEach var="op" items="${answer.question.options }">
  <li>
    <div q-id="${answer.question.id }" option-id="${op.id }" result-id="${answer.question.result }" class="zm" type="${answer.question.type.val }">${op.title }</div>
    <div class="tm">${op.txt }</div>
  </li>
</c:forEach>







四.



五.



六.



七.




1 0
原创粉丝点击