如何在onkeyup和onkeydown中使用ActionResult输入哪种类型是数字



我正在尝试在输入函数中使用操作结果,这是我的代码,我的目标是当用户Keyup或keydown或在输入中键入数字时,产品价格的总和会发生变化。我不知道哪一个事件适合我的目标,我使用了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; }
}

还有另一个问题?当用户在输入中键入数字时,我应该使用哪个事件?onChange((?还是别的?

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

最新更新