将ENTER键重新定义为TAB键

时间:2017-04-21 14:30:12

标签: r shiny textinput

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

1 个答案:

答案 0 :(得分:0)

请参阅以下示例:

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一起包含在内。