清除带有退格按钮的 selectizeInput 框 (maxItems = 1) 和 Mozilla Firefox 的问题

Clear selectizeInput box with backspace button (maxItems = 1) and troubles with Mozilla Firefox

我有一个带有多个 selectizeInput 小部件的 shinyapp。对于其中的大多数,用户只能输入一个选择。我的问题:

下面是我的 shinyapp 中存在的 selectizeInput 小部件之一的示例:

selectizeInput(inputId = "country_study",
               label = h6("Country"),
               choices = c("China" = "",
                           "China", "India", "Iran"),
               selected = "",
               multiple = FALSE,
               options = list(create = FALSE))

回顾一下,用户可以select只有一个选择,但我想让他能够直接用键盘上的退格键删除这个选择无需通过鼠标再次激活小部件。

如果这不可能,我该如何处理 Mozilla Firefox 上的这种奇怪行为?

非常感谢!

感谢另一个 Whosebug post,我找到了解决方案,您可以找到 here.

在您的 fluidPage 的开头只需添加:

tags$head(includeScript("disable_backspace.js")),

在您的 App 目录中,使用文本编辑器创建 "disable_backspace.js" 文件并将以下代码粘贴到其中:

$(function(){
/*
 * this swallows backspace keys on any non-input element.
 * stops backspace -> back
 */
var rx = /INPUT|SELECT|TEXTAREA/i;

$(document).bind("keydown keypress", function(e){
    if( e.which == 8 ){ // 8 == backspace
        if(!rx.test(e.target.tagName) || e.target.disabled || e.target.readOnly ){
            e.preventDefault();
        }
    }
}); });

此解决方案归功于 thetoolman