从文本框onblur更改为onKeyDown事件

时间:2018-01-09 16:37:04

标签: javascript razor-pages

我的部分视图中有以下代码:

    @foreach (var lineItem in Model.LineItems)
{
    <tr>
        <td>
            <img src="@Url.Action("GetImage", "ImageBlocks", new { imageID = lineItem.Product.SelfOrDefault().Image.SelfOrDefault().ImageId, Width = 75 })" alt="Thumbnail" />
        </td>
        <td>
            <span title="@(lineItem.Description ?? "")" id="lineItemDescription">@(lineItem.Product.ReceiptName ?? "")</span><br />
            <span id="lineItemSKU">@lineItem.Product.SKU</span>
        </td>
        <td>
            <span>@((lineItem.Product.InventorySummary ?? 0).ToString())</span><br />
        </td>
        <td><input type="text" id="lineItemQuantity" name="lineItemQuantity" value="@lineItem.Quantity" onblur="changeCartQty(@lineItem.Product.SKU,this.value)" /></td>
        @if (lineItem.Discount != null ? lineItem.Discount.DiscountCategory != DiscountCategory.ORDER : true)
        {
            <td>
                <input type="text" id="lineItemAmount" name="lineItemAmount" value="@lineItem.AdjustedUnitPrice.Value.ToString("C")" /><br />(Adj. @(lineItem.DiscountDisplayValue))
            </td>
            <td>@lineItem.AdjustedTotalLinePrice.Value.ToString("C")</td>
        }
        else
        {
            <td>@lineItem.BaseUnitPrice.Value.ToString("C")</td>
            <td>@lineItem.TotalLineBaseValue.ToString("C")</td>
        }
        <td><a href="#" class="removeItem" value="@lineItem.Product.SelfOrDefault().SKU">Remove</a></td>
    </tr>
}
<script>

在lineItemQuantity文本输入中,我想将其从onblur更改为onKeyDown事件,以捕获按下的“Enter”键以调用具有相同参数的changeCartQty函数。这是被调用的函数:

function changeCartQty(ProductId, Quantity) {
var userId = $('#ID').val();
$.ajax({
    url: "/Orders/AddtoCart",
    type: 'POST',
    cache: false,
    datatype: 'json',
    async: false,
    data: { "ProductID": ProductId, "Quantity": Quantity, "UserID": userId},
    success: function (data) {
        console.log("Adjusted Quantity in cart");
        success(data);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        console.error("[Error in Ajax request, adjust price] Code:" + jqXHR.status + "Error: " + errorThrown + " \nText Status: " + jqXHR.resonseText);
    }
})

};

这样工作正常,金额更新,我只需将其更改为onChange,我不确定如何做到这一点。

感谢。

0 个答案:

没有答案