2017-11-02 10 views
-1

私は私のカートアイテムの数量を更新するには、次のコードを持っている:ショッピングカートアップデート数量onKeyUpを

<div class="cart-col cart-col-qty" data-caption="Quantity"> 
<div id="[email protected]_id"> 
    @Html.TextBoxFor(model => model.CartItems[ix].Count, 
      new 
      { 
       @class = "test11 form-control", 
       @type = "text", 
       @min = "0" 
      }) 
    <a href="#" class="RefreshQuantity tessss btn btn-danger btn-to-danger btn-sm btn-icon" data-id="@item.item_id" 
     txt-id="[email protected](ix)__Count"><i class="fa fa-2x">+</i></a>&nbsp;&nbsp; 
</div> 

私はこのコードでanchor要素をクリックすると、POSTリクエストを作るために、AJAXを使用しています:

私は anchorにクリック機能を削除し、cartItem IDと数量のウィットでAJAXリクエストを作るために、入力のOnKeyUpイベントを使用したい
$(".RefreshQuantity").click(function() { 
// Get the id from the link 
var recordToUpdate = $(this).attr("data-id"); 
var countToUpdate = $("#" + $(this).attr("txt-id")).val(); 
if (recordToUpdate != '') { 
    // Perform the ajax post 
    $.post("/ShoppingCart/UpdateCartCount", { "id": recordToUpdate, "cartCount": countToUpdate }, 
     function (data) { 
      // Successful requests get here 
      // Update the page elements 
      if (data.ItemCount == 0) { 
       $('#row-' + data.DeleteId).fadeOut('slow'); 
      } 
      location.reload();     
     }); 
} 
}); 

ページのリフレッシュ。どうすればこれを達成できますか?

答えて

0

あなたは

$("#YourElement").on('keyup', function (e) { 
     e.preventDefault(); //This will avoid post form 
     if (e.keyCode === 13) { //This evaluates enter key 
      //Your logic 
     } 
    }); 
+0

私のコードスニペットを見て再び代わりに.clickの本を使用し、あなたの答えは –

+0

変更をどのように動作するか私に言うことができるこの:$(「RefreshQuantity。」)((機能をクリック)します。これは:$( "#YourElement")。on( 'keyup'、function(e)、基本的に... – JCM