【问题标题】:Redefining the ENTER key into a TAB key将 ENTER 键重新定义为 TAB 键
【发布时间】:2017-09-18 14:34:45
【问题描述】:

如何使 ENTER 像 TAB 一样,即当用户在输入字段中按 ENTER 时,光标会跳到下一个字段,就像按 TAB 键一样?

【问题讨论】:

    标签: r shiny textinput


    【解决方案1】:

    请看下面的例子:

    library(shiny)
    library(shinyjs)
    # Define UI for application that draws a histogram
    ui <- fluidPage(
       useShinyjs(),  
       sidebarLayout(
          sidebarPanel(
             textInput("txt1", "Text: "),
             textInput("txt2", "Text: ")         
          ),
          mainPanel()
       )
    )
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
     observe({
       runjs("
      var inputs = $(':input').keypress(function(e){ 
        if (e.which == 13) {
             e.preventDefault();
             var nextInput = inputs.get(inputs.index(this) + 1);
             if (nextInput) {
             nextInput.focus();
             }
         }
        });
      ")
     })  
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    javascript 来自 here,并包含在闪亮的 shinyjs 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-08
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多