【问题标题】:How can I return the ID of the focused element in R Shiny?如何在 R Shiny 中返回焦点元素的 ID?
【发布时间】:2019-06-22 12:16:08
【问题描述】:

我在 R Shiny 中有一系列 textInputs,并希望在 textOutput 中获取并显示具有焦点的文本框的 ID,即光标闪烁的那个。

我正在尝试在 JavaScript 中执行此操作,但收效甚微。

这是我一直在使用的:

ui <- fluidPage(
  tags$script(' Shiny.setInputValue("focused.element", $(document.activeElement )) '),
  textInput(inputId = "text1", label = NULL, value = ""),
  textInput(inputId = "text2", label = NULL, value = ""),
  textInput(inputId = "text3", label = NULL, value = ""),
  textInput(inputId = "text4", label = NULL, value = ""),
  textOutput("output1")
)

server <- function(input, output, session) {
  output$output1 <- renderText({ input$focused.element })
}

我希望当光标位于第一个 textInput 时显示“text1”,当光标位于第二个时显示 text2,等等...

现在,输出 1 中没有显示任何文本。任何帮助将不胜感激!

【问题讨论】:

    标签: javascript shiny


    【解决方案1】:

    这个?

    library(shiny)
    ui <- fluidPage(
      tags$script('$(document).ready(function(){ $("input").on("focus", function(e){ Shiny.setInputValue("focusedElement", e.target.id);}); }); '),
      textInput(inputId = "text1", label = NULL, value = ""),
      textInput(inputId = "text2", label = NULL, value = ""),
      textInput(inputId = "text3", label = NULL, value = ""),
      textInput(inputId = "text4", label = NULL, value = ""),
      textOutput("output1")
    )
    
    server <- function(input, output, session) {
      output$output1 <- renderText({ input$focusedElement })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-20
      • 2023-02-04
      • 2017-12-18
      • 2018-07-05
      • 2022-07-05
      • 2012-04-17
      • 2018-03-25
      • 2017-07-07
      相关资源
      最近更新 更多