嗨,我在jquery中有此变量,它获取选中的输入单选按钮的值,
library(shiny)
# Define UI for data download app ----
ui <- fluidPage(
# App title ----
titlePanel("Downloading Data"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Choose dataset ----
selectInput("dataset", "Choose a dataset:",
choices = c("rock", "pressure", "cars")),
checkboxInput("quotes",
strong("Include quotes around values"),
value = F),
# Button
downloadButton("downloadData", "Download")
),
# Main panel for displaying outputs ----
mainPanel(
tableOutput("table")
)
)
)
# Define server logic to display and download selected file ----
server <- function(input, output) {
# Reactive value for selected dataset ----
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
})
# Table of selected dataset ----
output$table <- renderTable({
datasetInput()
})
# Downloadable csv of selected dataset ----
output$downloadData <- downloadHandler(
filename = function() {
paste(input$dataset, ".csv", sep = "")
},
if(input$quotes==F){
content = function(file) {
write.csv(datasetInput(), file, row.names = FALSE,quote = F)
}
}
else{
content = function(file) {
write.csv(datasetInput(), file, row.names = FALSE,quote = T)
}
}
)
}
# Create Shiny app ----
shinyApp(ui, server)
我的输入字段如下:
var gender = [];
$("input[type='radio'][name='gender\\[$counter\\]']:checked").each(function ()
{
gender.push($(this).val());
});
console.log(gender);
但是console.log(gender);返回值“ []”,但不返回该输入字段的值。似乎是什么问题?请帮忙。
答案 0 :(得分:1)
由于您将性别定义为数组[] ...因此将性别显示为数组[]。
为什么性别是数组?性别不分男女,不超过一个。因此,从
开始var gender = "";
然后无需遍历所有元素,因为将仅检查一个元素。为了确保并避免出现异常,请先检查是否检查了收音机:
if($("input[type='radio'][name='gender\\[$counter\\]']:checked").length > 0){
gender = $("input[type='radio'][name='gender\\[$counter\\]']:checked").val();
}