如何调用ajax方法

时间:2013-04-02 11:08:00

标签: jquery ajax

我有以下代码

<li id="btemaildelete"><a href="##" onClick="return confirm('Are you sure you want to delete this Email Address?');"><i class="cus-cancel"></i> Delete</a></li>

并想打电话给以下

emaildelete.cfm?intuserid=1

你能帮我理解一下吗?

4 个答案:

答案 0 :(得分:1)

如果你有jQuery包括:

$.get("emaildelete.cfm", {intuserid: 1}, function(data) {
    // data contains the response of emaildelete.cfm
});

要在jQuery中捕获click事件,您可以执行以下操作:

HTML:
<a href="..." data-id="1">Delete</a>

JavaScript:
$(document).ready(function() {
    $("a[data-id]").click(function() {
       var confirm = confirm("Are you sure u wanna delete?");
       if(!confirm) return false;

       var deleteId = $(this).data("id");
       $.get("emaildelete.cfm", {intuserid: deleteId}, function(data) {
           // data contains the response of emaildelete.cfm
       });

       return false;
    });
});   

使用该代码,您不再需要“onclick”属性。

答案 1 :(得分:0)

这是一个例子

$.ajax({
    type: 'POST',
    url: 'url',
    data: '{ }',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function(msg) {
        // Do something interesting here.
    }
});

答案 2 :(得分:0)

试试这样:

<li id="btemaildelete"><a href="##" onClick="call_function();"><i class="cus-cancel"></i> Delete</a></li>

在您的javascript代码中:

function call_function() {
    if(confirm('Are you sure you want to delete this Email Address?'))
     {
     $.ajax({
     url: "emaildelete.cfm?intuserid=1",  //Or any other server page you want
     type: "POST",
     success: function (response) {
      alert('Success' + response);
     }
    });

答案 3 :(得分:0)

你必须从链接本身传递id,不确定你是如何通过aspx诚实地填充这些链接的。你只需要将电子邮件的id输入到链接的id中,但是你的应用程序可以这样做。

<li class="btemaildelete"><a href="##" class="deleteLink" id="get your ID in here" 
onClick="return confirm('Are you sure you want to delete this Email Address?');"><i 
class="cus-cancel"></i> Delete</a></li>

$(".deleteLink").on("click",function(){
$.ajax({
    type: 'POST',
    url: 'emaildelete.cfm',
    data: '{ intuserid : $(this).attr("id") }',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function(msg) {
        alert('Email deleted');
    }
});
});