簡體   English   中英

如何使用Javascript獲取多個輸入字段的值

[英]How to get values of multiple Input fields using Javascript

我正在嘗試使用PHP和AJAX創建一個電子商務網站,但我對此感到困惑。 我在獲取用戶購物車所有輸入字段的值時遇到問題。 換句話說,假設用戶的購物車中有3件商品,每件商品的數量為“ 1”。 現在,假設用戶增加了購物車中第二個物品的數量並將其更改為“ 3”,然后單擊“更新購物車”按鈕。 單擊更新購物車時,我想要的只是占用已更新的值,而不占用其他值。 如果那不可能,我想獲取所有值,然后相應地更新數據庫。 當前,當我單擊“更新購物車”按鈕時,它僅占用購物車中第一項的數量值。

下面是PHP代碼(請不要介意使用的變量)

echo "<tr>
        <td class='p-image'>
          <a href='product-details?id=$fetchProductID'><img alt='' src='$theRealLink'></a>
        </td>
        <td class='p-name'><a href='product-details?id=$fetchProductID'>$theName</a></td>
        <td class='p-amount'>INR $fetchProductUnitPrice</td>
        <td class='p-quantity'><input maxlength='100' type='text' value='$fetchProductQuantity' name='quantity' class='productQuantity' data-quant='$fetchProductQuantity'></td>
        <td class='p-total'><span>INR $fetchedProductTotal </span></td>
        <td class='edit'><a href='javascript:void(0)' class='delete-from-cart' data-id='$fetchProductID'><img src='assets/img/icon/delte.png' alt=''></a>
        </td>
      </tr>";

而且AJAX代碼在下面

$('#update-cart').click(function() {
    var quantity = $('.productQuantity').val();
    console.log(quantity);
});

我可能會去更新所有這些。 您可以使用jQuery的每個功能

$(".productQuantity").each(function() {
    var quantity = $(this).val();
    console.log(quantity);
    // Get the data-id and build an array so you submit only 1 ajax request
});

我不確定我的解釋是否正確,但是如果您要更新數據庫中的值,該值引用了購物車中的某個項目,則意味着您還需要確定哪個項目是該項目。正在更新,但我不太清楚提供的代碼是否清楚。

無論如何,您可以在數量字段上添加“ onkeyup”事件,並為該數量所屬的“項目”設置一個標志,或將其存儲在要更新的項目數組中,然后按“更新購物車”,然后將修改后的項目發送到服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM