javascript - 在 asp.net mvc 中更新文本框值的脚本不起作用

标签 javascript jquery ajax asp.net-mvc

我想在 asp.net MVC5 中使用 Ajax 更新文本框的值(包含 cookie 的值)。我对 JavaScript 很陌生,我编写了这些代码,但我的代码不起作用。我没有收到任何错误,但它不起作用。我在外部文件“UpdateTxtBox.js”中编写了 JavaScript,并添加了 <script src="~/Scripts/UpdateTxtBox.js"></script>计划 。 谁能告诉我出了什么问题吗?

$(function () {
$("textCountProduct").change(function () {
    var count = $(this).val();
    var id = $(this).attr("productid");
    $.ajax({
        url: "/Goods/AddToCart",
        data: { Id: id, Count: count },
        type: "Post",
        dataType: "Json",
        success: function (result) {
            if (result.Success) {
                alert(result.Html);
                $("#CartItems").html(result.Html);
            }
            eval(result.Script);
        },
        error: function () {
            alert("error....");
        }
    });
});
});

Basket.cshtml 的一部分

@using (Html.BeginForm("AddToCart", "Goods", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
   @Html.TextBoxFor(modelItem => item.Count, new { @class="text textCountProduct" , style="width:40px;" , productid=item.GoodDetails.DetailsGoodID})

 }

良好的 Controller

public ActionResult AddToCart (int Id , int Count)
    {
        try
        {
            if (Request.Cookies.AllKeys.Contains("NishtmanCart_" + Id.ToString()))
        {
            //Edit cookie
            var cookie = new HttpCookie("NishtmanCart_" + Id.ToString(), (Convert.ToInt32(Request.Cookies["NishtmanCart_" + Id.ToString()].Value) + 1).ToString());
            cookie.Expires = DateTime.Now.AddMonths(1);
            cookie.HttpOnly = true;
            Response.Cookies.Set(cookie);

        }
        else
        {
            //Add new cookie
            var cookie = new HttpCookie("NishtmanCart_" + Id.ToString(), Count.ToString());
            cookie.Expires = DateTime.Now.AddMonths(1);
            cookie.HttpOnly = true;
            Response.Cookies.Add(cookie);
        }
            List<HttpCookie> lst = new List<HttpCookie>();
            for (int i = 0; i < Request.Cookies.Count; i++ )
            {
                lst.Add(Request.Cookies[i]);
            }

            bool isGet = Request.HttpMethod == "GET";
            int CartCount = lst.Where(p => p.Name.StartsWith("NishtmanCart_") && p.HttpOnly != isGet).Count();
            return Json(new MyJsonData()
            {
                Success = true,
                Script = MessageBox.Show("Good added successfully", MessageType.Success).Script,
                //Script = "alert('Good added successfully');",
                Html = "cart items  (" + CartCount.ToString() + ")"
            }
                );
        }

更新帖子: 我将 [HttpPost] 添加到 Controller 操作结果并向 javascript 添加一些警报

$(function () {
alert("aleeeert");
$(".textCountProduct").change(function () {
    var count = $(this).val();
    var id = $(this).attr("productid");
    alert(count);
    alert(id);
    $.ajax({
        url: "/Goods/AddToCart",
        data: { Id: id, Count: count },
        type: "Post",
        dataType: "Json",
        success: function (result) {
            if (result.Success) {
                alert(result.Html);
                $("#CartItems").html(result.Html);
            }
            eval(result.Script);
        },
        error: function () {
            alert("error....");
        }
    });
});
});

工作正常,但当我刷新页面时,数据未保存

最佳答案

由于您已将 textCountProduct 指定为 CSS 类,因此需要在其前面加上 . 前缀才能使用 Class Selector (“.class”) ,到目前为止,它正在寻找显然不存在的元素textCountProduct

使用

$(".textCountProduct").change(

关于javascript - 在 asp.net mvc 中更新文本框值的脚本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34894618/

相关文章:

javascript - val() 返回 [object 对象]

jquery - 将 jQuery .click() 应用于 Ajax 生成的 HTML

javascript - 取消设置 ajax 中通过 post 传递的变量

javascript - 我最初尝试让默认路由在 ember 中工作时缺少什么?

javascript - 如何获取对象类型 SVGComponentTransferFunctionElement

javascript - 旋转 HTML5 视频并保存 Canvas

javascript - 等待动态脚本加载的 JS 类加载

JQuery $this 选择器 - 如何使其工作

asp.net - 与 ASP.NET 和 SQL Server 聊天

php - 滚动条自动隐藏,除非我调整页面大小