Ajax仅在刷新前一次在相同的输入字段中发布(ASP.NET)

时间:2016-01-25 18:20:44

标签: jquery sql asp.net ajax vb.net

我正在开设一个电子商务网站,其中列出了多个产品,每个产品都有自己的输入字段和提交带有唯一ID的按钮。当我向购物车添加不同的商品时,它工作正常,但是,当我在进行页面刷新之前多次添加相同的商品时,它不会发布帖子。

这是我的AJAX代码:

function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}

var debug = false;

$(document).ready(function () {


$('#productcontent').on('click', 'input[type="submit"]', function (event) {

            event.preventDefault();
            var inputID = this.name;
            //alert($("#" + inputID).val());
        var qty = $("#"+inputID).val();

        if (!isNumber(qty)) {
            $("#"+inputID).focus();
            return;

        }
        var userQuantityText = $('#cart' + inputID).text();
        var userQuantity = userQuantityText.slice(0,1);
        var userQuantityInt = parseInt(userQuantity);
        var combinedQty = userQuantityInt + parseInt(qty);

        $.ajax({
                    type: "POST",
                    url: "pladditem.aspx",
                    data: { shoppingCartItemNum: inputID, shoppingCartItemQuan: qty },
                    success: function (htmldata) {
                $("#"+inputID).val("");
                $(".notifications").replaceWith("<span class='notifications'>" + htmldata + "</span>");
                if ($('#cart' + inputID).length > 0) {
                    $('#cart' + inputID).replaceWith('<div id="cart' + inputID + '" class="items-in-cart">' + combinedQty + ' item(s) in cart</div>');
                }
                else {
                    $('<div id="cart' + inputID + '" class="items-in-cart">' + qty + ' item(s) in cart</div>').insertBefore("#" + inputID);
                }
                $('<div style="background-color:yellow;">' + "Added " + qty + " to your shopping cart!" + '</div>').insertBefore("#" + inputID).delay(3000).fadeOut();
                        //alert("Added " + qty + " to your shopping cart with " + htmldata + " items! Item #: " + inputID);

                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                       if (debug) {
                           alert(XMLHttpRequest.responseText);
                           alert(textStatus);
                           alert(errorThrown);
                      }
                    }
                });


});

$('.quanityinput').keypress(function (event) {

    if (event.which == 13) {
        event.preventDefault();
        var inputID = this.name.slice(1,9);
        //alert(inputID);
        var qty = $("#" + inputID).val();
        //alert(qty);

        if (!isNumber(qty)) {
            $("#" + inputID).focus();
            return;

        }

        var userQuantityText = $('#cart' + inputID).text();
        var userQuantity = userQuantityText.slice(0, 1);
        var userQuantityInt = parseInt(userQuantity);
        var combinedQty = userQuantityInt + parseInt(qty);

        $.ajax({
            type: "POST",
            url: "pladditem.aspx",
            data: { shoppingCartItemNum: inputID, shoppingCartItemQuan: qty },
            success: function (htmldata) {
                $("#" + inputID).val("");
                $(".notifications").replaceWith("<span class='notifications'>" + htmldata + "</span>");
                if ($('#cart' + inputID).length > 0) {
                    $('#cart' + inputID).replaceWith('<div id="cart' + inputID + '" class="items-in-cart">' + combinedQty + ' item(s) in cart</div>');
                }
                else {
                    $('<div id="cart' + inputID + '" class="items-in-cart">' + qty + ' item(s) in cart</div>').insertBefore("#" + inputID);
                }
                $('<div style="background-color:yellow;">' + "Added " + qty + " to your shopping cart!" + '</div>').insertBefore("#" + inputID).delay(3000).fadeOut();
                //alert("Added " + qty + " to your shopping cart with " + htmldata + " items! Item #: " + inputID);

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                if (debug) {
                    alert(XMLHttpRequest.responseText);
                    alert(textStatus);
                    alert(errorThrown);
                }
            }
        });
    }




});


});

这是在后端运行的VB.NET代码:

Partial Class pladditem
Inherits System.Web.UI.Page
Dim connection As New Data.SqlClient.SqlConnection
Dim query As New Data.SqlClient.SqlCommand

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Session("Username") <> "" Then

        Dim shoppingcartitems As String = ""


        Dim itemno As String = ""
        itemno = Request.Form("shoppingCartItemNum")
        Dim itemqty As String = ""
        itemqty = Request.Form("shoppingCartItemQuan")

        connection.ConnectionString = "Data Source=blah blah blah;Initial Catalog=blah;User ID=blah;password=blah"
        query.Connection = connection
        query.Parameters.Clear()
        '''' CHANGES QUANTITY FOR A SINGLE ITEM ON THE ORDER
        query.Connection = connection
        query.Parameters.Clear()
        query.Parameters.Add("@USERNAME", Data.SqlDbType.VarChar).Value = Session("Username")
        ''' WILL PROBABLY NEED TO BE CHANGED
        query.Parameters.Add("@QUANTITY", Data.SqlDbType.VarChar).Value = Request.Form("shoppingCartItemQuan")
        query.Parameters.Add("@ITEM", Data.SqlDbType.VarChar).Value = Request.Form("shoppingCartItemNum")
        ''''''''''''''''''''''''''''''''''''
        query.CommandText = "exec Web_AddToCart @USER = @USERNAME, @QTY = @QUANTITY, @ITEMNO = @ITEM"
        query.Connection.Open()
        query.ExecuteNonQuery()
        query.Connection.Close()

        query.CommandText = "select count('1') from weboeordd where orduniq not in (select orduniq from weboeordsubmit) and  orduniq in (select orduniq from weboeordh where [user] = @username)"
        Dim dt As New Data.DataTable
        Dim da As New Data.SqlClient.SqlDataAdapter(query)
        da.Fill(dt)
        If dt.Rows.Count > 0 Then
            Response.Write(dt.Rows(0).Item(0).ToString)
        Else
            Response.Write("")
        End If


    End If

End Sub
End Class

1 个答案:

答案 0 :(得分:0)

这听起来很愚蠢,但我完全忘记了我运行的SQL存储过程是如此设置所以它等待10秒再次执行SQL查询。我将时间改为3秒,现在很好。