在R Shiny中使用带有操作按钮的回车键 [英] Using enter key with action button in R Shiny

查看:1871
本文介绍了在R Shiny中使用带有操作按钮的回车键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个javascript函数来扩展R闪亮的动作按钮演示。我希望用户能够通过单击操作按钮并在选择数字输入表单时按Enter键来输入数字。 ui.R的代码是:

I am trying to write a javascript function to extend the R shiny action button demo. I would like the user to be able to enter a number by both clicking the action button and by hitting enter when they have selected the number input form. The code for ui.R is:

shinyUI(pageWithSidebar(
  headerPanel("actionButton test"),
  sidebarPanel(
    tags$head(tags$script(src = "enter_button.js")), 
    numericInput("number", "N:", min = 0, max = 100, value = 50),
    br(),
    actionButton("goButton", "Go!"),
    p("Click the button to update the value displayed in the main panel.")
  ),
  mainPanel(
    verbatimTextOutput("nText")
  )
))

和server.R:

shinyServer(function(input, output) {
 ntext <- eventReactive(input$goButton, {
    input$number
  })

  output$nText <- renderText({
    paste(ntext(), input$goButton)
  })
})

I在www /文件夹中名为enter_button.js的文件中包含以下javascript:

I included the following javascript in a file called enter_button.js within the www/ folder:

$("#number").keyup(function(event){
    if(event.keyCode == 13){
        $("#goButton").click();
    }
});

然而,当我运行应用程序时,选择数字输入表单并点击进入操作按钮不得到增量,但如果我点击它会增加。或者,我也测试了只需按下输入文档的任何位置:

However, when I run the app, select the number input form and hit enter the action button does not get incremented, but it does get incremented if I click it. Alternatively, I also tested just hitting enter anywhere on the document with:

$(document).keyup(function(event){
    if(event.keyCode == 13){
        $("#goButton").click();
    }
});

这是有效的,所以我怀疑jQuery找不到#number元素。在此先感谢!

and that worked, so my suspicion is that the jQuery cannot find the #number element. Thanks in advance!

推荐答案

我能够使用jQuery 来解决这个问题(:focus )函数,我使用的代码是:

I was able to figure this out using the jQuery is(":focus") function, the code I used was:

$(document).keyup(function(event) {
    if ($("#number").is(":focus") && (event.key == "Enter")) {
        $("#goButton").click();
    }
});

这篇关于在R Shiny中使用带有操作按钮的回车键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆