簡體   English   中英

Magento 2 - 如何在 phtml 文件中傳遞輸入值以阻止 class?

[英]Magento 2 - How to pass input value in phtml file to block class?

我正在嘗試用我的自定義代碼覆蓋 Magento 模塊塊,包括 phtml 文件和塊中的 php class。 在我的 phtml 文件中,我添加了一個輸入字段,我想將輸入的值傳遞給塊 class,這樣我就可以使用該值來更新數據庫。

So far, I have created a button and an onClick php function called by the button, but I have no idea how to pass the input value to that function. 我曾嘗試使用document.getElementById ,但它不起作用。

php function 是:

public function updatePayment($paidAmount)
{
    $currentPaid = $this->getSource()->getTotalPaid();
    $this->getOrder()->setTotalPaid($paidAmount + $currentPaid)->save();
}

phtml文件輸入HTML:

<input id="update-payment-input" type="text" style="text-align:right;" value="<?= $block->setDefaultAmountForPayment() ?>">
<script>
    require(['prototype'], function() {
        document.getElementById("update-payment-submit").addEventListener("click", updateAmount);
        function updateAmount() {
            <?= $block->updatePayment() ?>
            // Here I tried to use document.getElementById to get the input value
        }
    });
</script>

我希望該值被捕獲並傳遞給 class Block中的 function,但它顯示一個錯誤,指出該文檔未定義。

將此代碼添加到您的 HTML 文件中。

<input id="update-payment-input" type="text" style="text-align:right;" value="<?= $block->setDefaultAmountForPayment() ?>" onclick="updateAmount(this.value)">
<script>
    function updateAmount(value) {
        <?= $block->updatePayment(value) ?>
    }
</script>

暫無
暫無

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

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