【问题标题】:Jquery Ajax Call on click of <a> href element点击 <a> href 元素的 Jquery Ajax 调用
【发布时间】:2015-11-18 21:50:33
【问题描述】:

这是我的 aspx 页面。当我点击 a href 元素时,我需要使用 jquery ajax 在代码隐藏中调用一个函数。

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="kodeeswaranKBC.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>KFC</title>

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body style="background-color: black">
<form id="form1" runat="server">
    <div class="container-fluid">
        <div class="row">
            <div class="col-sm-4"></div>
            <div class="col-sm-4" style="text-align: center">
                <img src="img/logo.jpg" style="height: 185px; width: 185px" />
            </div>
            <div class="col-sm-4"></div>
        </div>
        <div class="row">
            <div class="col-lg-12" style="height: 30px"></div>
        </div>
        <div class="row">
            <div class="col-sm-12" style="height: 100px">

                <div style="background-image: url(img/question.png); height: 100%; background-repeat: no-repeat; background-size: contain;background-position: center;">
                    <asp:Label ID="question" runat="server" Text="Question" Font-Bold="true" Style="color:white;position: absolute; left: 200px; top: 27px"  ></asp:Label>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12" style="height: 30px"></div>
        </div>
        <div class="row">
            <div class="col-lg-6" style="height: 80px">
                <a href="#" id="op1">
                    <div style="background-image: url(img/answer_left.png); height: 100%; background-repeat: no-repeat; background-size: contain; background-position: center;">
                        <asp:Label ID="option1" runat="server" Text="Option1" Font-Bold="true" Style="position: absolute; left: 200px; top: 27px"></asp:Label>
                    </div>
                </a>

            </div>
            <div class="col-lg-6" style="height: 80px">
                <a href="#" id="op2">
                    <div style="background-image: url(img/answer_right.png); height: 100%; background-repeat: no-repeat; background-size: contain; background-position: center;">
                        <asp:Label ID="option2" runat="server" Text="Option2" Font-Bold="true" Style="position: absolute; left: 85px; top: 27px"></asp:Label>
                    </div>
                </a>
            </div>
            <div class="row">
                <div class="col-lg-12" style="height: 30px"></div>
            </div>

        </div>

        <div class="row">
            <div class="col-lg-6" style="height: 80px">
                <a href="#" id="op3">
                    <div style="background-image: url(img/answer_left.png); height: 100%; background-repeat: no-repeat; background-size: contain; background-position: center;">
                        <asp:Label ID="option3" runat="server" Text="Option3" Font-Bold="true" Style="position: absolute; left: 200px; top: 27px"></asp:Label>
                    </div>
                </a>

            </div>
            <div class="col-lg-6" style="height: 80px">
                <a href="#" id="op4">

                    <div style="background-image: url(img/answer_right.png); height: 100%; background-repeat: no-repeat; background-size: contain; background-position: center;">
                        <asp:Label ID="option4" runat="server" Text="Option4" Font-Bold="true" Style="position: absolute; left: 85px; top: 27px"></asp:Label>
                    </div>
                </a>
            </div>

        </div>
    </div>

</form>
</body>
</html>
<script>
$(document).ready(function () {
    alert("hai");
    $("#op1").click(function (e) {
        e.preventDefault();
        $.ajax({
            type: "GET",
            url: "index.aspx/mymethod",
    //contentType: "application/json; charset=utf-8",
    //dataType: "json",
    //success: OnSuccess,
    //failure: function (response) {
    //    alert(response.d);
    //}
         });
    });

    //$("#op1").click(function () {
    //    alert("ssssss");
    //});
});
</script>

在 .aspx.cs 文件中

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace kodeeswaranKBC
{
public partial class index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        StartGame();

    }

    protected void StartGame()
    {
        question.Text = "Here comes your Question";
        option1.Text = "This is Option1";
        option2.Text = "This is option2";
        option3.Text = "This is option3";
        option4.Text = "This is option4";
    }

    [WebMethod]
    public static void mymethod()
    {

    }

}
}

如何调用 mymethod?我是 ajax 新手。请检查我的代码并给我答复。如果我的问题不清楚,请告诉我

【问题讨论】:

标签: asp.net ajax asp.net-ajax


【解决方案1】:

为了在代码隐藏 aspx web 表单中调用方法,您需要记住几件事。

  1. 方法必须是静态的。
  2. 方法需要公开。
  3. 方法需要用属性[webmethod]标记。

所以在你的情况下,方法看起来像:

[Webmethod]
public static void mymethod()
{

}

从 jquery 看其余的实现看起来不错,只需输入 e.preventDefault() 以禁用锚标记的默认行为。

编辑:

$("#op1").click(function (e) {
    e.preventDefault();
    $.ajax({
        type: "GET",
        url: "index.aspx/mymethod",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
       success: function (response) {
          alert(response.d);
       },
       error: function (error) {
          alert();
      }
     });
});

【讨论】:

  • 谢谢。但是调用不会转到mymethod。相反,它调用页面加载函数本身。可能是什么原因?
  • 您需要使用 e.preventDefault(); 防止锚标签在点击时重新加载;更新解决方案
  • 我做了这些改变。但仍然调用页面加载功能。在进行此更改之前,这是我添加的 ajx 库参考。
  • 将类型:“POST”更改为:“GET”
  • 我也试过这个。但仍然是同样的问题。稍后我需要将值传递给方法。所以我认为我应该使用 post 方法。我有什么选择可以和你分享我写的整个代码吗?
猜你喜欢
  • 2018-02-17
  • 2018-10-20
  • 2014-02-24
  • 2011-03-03
  • 2013-01-02
  • 2013-09-12
  • 2019-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多