簡體   English   中英

僅當在輸入框中輸入文本但未刪除文本時才如何觸發事件?

[英]How to fire an event only when text is entered in an input box but not removed?

當將數字輸入到9 * 9 Sudoku網格(或從中刪除)時,以下代碼片段觸發警報事件。

   $('table tr td input').on({"input": function () {
            var cell = $(this).val();

            if (!$.isNumeric(cell)) {
                alert("Please enter only numbers from 1 to 9");
            }

   }});

但是,如何在刪除文本時刪除警報消息? 僅當輸入數字時才必須觸發它。

更改

if (!$.isNumeric(cell)) {

if (cell && !$.isNumeric(cell)) {

我將執行以下操作。

$('table tr td input').on({"input": function () {
        var cell = $(this).val();

        if (cell.length > 0 && !$.isNumeric(cell)) {
            alert("Please enter only numbers from 1 to 9");
        }
}});

暫無
暫無

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

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