【问题标题】:Increment and decrements on button click using ajax使用ajax增加和减少按钮点击
【发布时间】:2017-01-25 14:20:26
【问题描述】:

我希望当我点击增量按钮时,分数会增加。我在服务器端代码中执行此操作,但在重新加载页面后它会增加。我希望它在不使用 ajax 和 jquery 重新加载页面的情况下做到这一点 这是我的代码

Deals.cs

[WebMethod]
[ScriptMethod]
public static string HOT(int DealPoints,int dealId)
{
    DealPoints++;
    int DealId = dealId;
    int Points = DealPoints;

    string retDeal = "";
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["FYP_Wish"].ToString()))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("IncrementDeals", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@DealPoints", SqlDbType.Int).Value = Points;
        cmd.Parameters.Add("@DealId", SqlDbType.Int).Value = DealId;
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        int result = cmd.ExecuteNonQuery();
        if (result == 1)
        {
            return "true";
        }
        con.Close();
        return retDeal;
    }
}

【问题讨论】:

  • 你的客户端代码在哪里?你已经尝试过了吗?
  • $(function () { $("[id*=HotButton]").bind("click", function () { var Deal = {}; Deal.DealPoints = $("[ id*=]").val(); Deal.DealId = $("[id*=]").val( ); $.ajax({ type: "POST", url: "Deals.aspx/HOT", data: '{DealPoints: ' + JSON.stringify(DealPoints) + ',DealId: ' + JSON.stringify(DealId) + '}', contentType: "application/json; charset=utf-8", d
  • 这里是但是当我点击按钮时它不起作用
  • 编辑您的问题并添加您的代码

标签: c# jquery asp.net json ajax


【解决方案1】:

试试这个:

$(document).ready(function(e) {
  $("#btn1").click(function(e) { //btn1 is button id
    var o = $(this).val();
    o++;
    $(this).val(o);
  });
});

【讨论】:

  • 欢迎来到 Stack Overflow。不鼓励仅使用代码的答案,因为它们没有解释如何解决问题中的问题。考虑更新您的答案以解释它的作用以及它如何解决问题。请查看How do I write a good answer
猜你喜欢
  • 1970-01-01
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 2018-01-12
  • 2020-09-21
  • 1970-01-01
  • 2021-09-16
  • 1970-01-01
相关资源
最近更新 更多