簡體   English   中英

將選擇元素設置為在表單提交(JQuery)時禁用

[英]Setting select element as disabled on form submission (JQuery)

我有一個php循環,它作為在線訂購系統的一部分,回顯了多個<select>元素,用戶可以在其中選擇特定項目的數量。

執行表單的按鈕運行一個php腳本,該腳本將值插入到我的數據庫中,但是,我需要能夠忽略等於“ 0”的值。

通過在提交按鈕上創建一個onClick事件(JS函數),我執行以下代碼:

$('#quantityForm').find(':select').each(function(){
    if($(this).val() == "0"){
        $(this).attr('disabled', 'disabled');
    }
})

除了將<select>元素設置為禁用的代碼外,其他一切似乎都可以正常工作,因為我的php腳本嘗試獲取所有“ 0”值。

我做錯什么了嗎?

附加代碼:

<form id="quantityForm" method="get">
    <?php get_items($example); ?>
    <button onClick="checkQuantity()" id='basket' type='submit' name='submit'><i class='fas fa-shopping-basket'></i></button>
</form>
$('#quantityForm')
    //find all the select elements in the form
    .find('select')
    //filter and return only those that have a value of 0
    .filter(function(){
        return this.value === '0';
    })
    //make them disabled
    .prop('disabled', true);

您可以在選擇value="0"的選項時刪除選擇的name屬性,這樣您的表單將忽略選擇輸入。

$(document).on('change', 'select', function() {
    if ($(this).val() == 0) {
        $(this).attr('name', '');
    }
    else {
        $(this).attr('name', '<name>');
    }
});

暫無
暫無

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

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