【发布时间】:2022-11-30 16:56:36
【问题描述】:
我想触发一个包含最后一次点击输入的输入 ID 的反应。我所拥有的对于numericInput 和textInput 等一些输入工作正常。但它不适用于 selectInput 或 selectizeInput。我试过在 JS 表达式中使用各种选择器,但没有一个选择器捕获 selectInput 或 selectizeInput。
这是一个代表。当您单击前两个输入中的任何一个时,renderText 会更新,但不会更新后两个。
library(shiny)
ui <- fluidPage(
tags$head(
tags$script(
htmlwidgets::JS("$( document ).on('click', '.form-control, .shiny-bound-input, .selectized', function() {
Shiny.setInputValue('last_input', this.id);
});")
)
),
numericInput("num1", "Numeric", 0),
textInput("text1", "Text"),
selectInput("select1", "Select", choices = LETTERS[1:4]),
selectInput("selectize1", "Selectize", choices = letters[1:4]),
textOutput("textout")
)
server <- function(input, output, session) {
output$textout <- renderText({
input$last_input
})
}
shinyApp(ui, server)
【问题讨论】:
-
我尝试了这个更通用的 JS 表达式,但它也没有用:
$(document).ready(function(){ $('input').on('click', function(evt){ Shiny.setInputValue('last_input', evt.target.id); }); }) -
查看开发者工具,好像shiny的select和selectize有
display: none,所以不会触发点击:<select id="select1" tabindex="-1" class="selectized shiny-bound-input" style="display: none;"><option value="A" selected="selected">A</option></select>
标签: javascript r shiny