【问题标题】:How to use ActionResult in onkeyup and onkeydown in a input which type is number?如何在输入类型为数字的 onkeyup 和 onkeydown 中使用 ActionResult?
【发布时间】:2020-08-04 10:05:10
【问题描述】:

我正在尝试在输入函数中使用操作结果,这是我的代码,我的目标是当用户 Keyup 或 keydown 或在输入中键入数字时,ProductPrice 的总和发生变化。我不知道,哪个事件是正确的为了我的目标,我使用了 onkeydown 和 onkeyup 事件。但没有任何改变。

  <td><input type="number" onkeyup="@Url.Action("CountUp","ShoppingCart",new { id = @item.ProductId })"
                         onkeydown="@Url.Action("CountDown","ShoppingCart",new { id = @item.ProductId })"
                                                                 
                         value="@item.ProductCount" min="0"  style="width:70px"></td>

这是我的控制器

      // GET: ShoppingCart
    public ActionResult Index()
    {
        List<ShowShoppingCart> shopcart = new List<ShowShoppingCart>();
        if (Session["ShoppingCart"] != null)
        {
            List<ShopCartItem> shop = Session["ShoppingCart"] as List<ShopCartItem>;
            foreach (var item in shop)
            {
                var product = db.Products.Find(item.ProductId);
                shopcart.Add(new ShowShoppingCart()
                {
                    ProductCount = item.ProductCount,
                    ProductId = item.ProductId,
                    ProductPrice = product.ProductPrice,
                    ProductTitle = product.ProductTitle,
                    Sum = item.ProductCount * product.ProductPrice


                });
            }
        }
        return View(shopcart);
    }

  public ActionResult CountUp(int id)
    {

        List<ShopCartItem> shop = Session["ShoppingCart"] as List<ShopCartItem>;
        int index = shop.FindIndex(s => s.ProductId == id);
        shop[index].ProductCount += 1;
        Session["ShoppingCart"] = shop;
        return RedirectToAction("Index");

    }

    public ActionResult CountDown(int id)
    {
        List<ShopCartItem> shop = Session["ShoppingCart"] as List<ShopCartItem>;
        int index = shop.FindIndex(s => s.ProductId == id);
        shop[index].ProductCount -= 1;
        if (shop[index].ProductCount == 0)
        {
            shop.Remove(shop[index]);
        }
        Session["ShoppingCart"] = shop;
        return RedirectToAction("Index");

    }

这是购物车

    public class ShopCartItem
{
    public int ProductId { get; set; }
    public int ProductCount { get; set; }
}

还有一个问题? 当用户在输入中输入数字时,我应该使用哪个事件?改变()?还是其他?

【问题讨论】:

    标签: javascript ajax asp.net-mvc model-view-controller


    【解决方案1】:

    不太确定你的主要问题,但关于第二个问题,你真的应该使用 onChange()

    【讨论】:

    • 谢谢,关于我的第一个问题,我有一张包含以下项目的发票:产品、尺寸、计数、价格和总和。当我单击计数输入时,我通过 keyup 和 keydown 更改它或在其中输入一个数字,我希望 sum 正确更改,但它没有改变,这是我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    相关资源
    最近更新 更多