在readonly textbox mvc4 razor中,禁用返回到前一页的退格

时间:2022-03-31 20:04:17

I have a datepicker field set to read only. I don't want the user to enter dates, but instead only select them from the calendar. This works fine. However, the moment I hit backspace in this readonly field, it redirects to the previous page. I would like to stop this functionality.

我有一个datepicker字段设置为只读。我不希望用户输入日期,而是从日历中选择它们。这是很好。然而,当我在这个readonly字段中单击backspace时,它将重定向到前一个页面。我想停止这个功能。

I have already included a "clear" button on the calendar for clearing purposes. However, when I still select all (the date) in this readonly field and try to clear (viz backspace) it does the above. Any assistance would be appreicated!!

我已经在日历上包含了一个“清除”按钮,用于清理。但是,当我在这个readonly字段中选择all(日期)并尝试清除(viz backspace)时,它会执行上述操作。任何帮助都将感激不尽!

1 个解决方案

#1


1  

The following appears to work in my tests, and you'll only prevent backspace from going back in the browser when in the actual input this way, instead of breaking the shortcut globally for the whole page.

下面的代码在我的测试中似乎是有效的,您将只防止backspace以这种方式返回到浏览器中,而不会破坏整个页面的全局快捷方式。

$('input[readonly]').on('keydown', function (e) {
    if (e.which === 8) {
        e.preventDefault();
    }
});

#1


1  

The following appears to work in my tests, and you'll only prevent backspace from going back in the browser when in the actual input this way, instead of breaking the shortcut globally for the whole page.

下面的代码在我的测试中似乎是有效的,您将只防止backspace以这种方式返回到浏览器中,而不会破坏整个页面的全局快捷方式。

$('input[readonly]').on('keydown', function (e) {
    if (e.which === 8) {
        e.preventDefault();
    }
});