如何将JavaScript变量值放在HTML输入标记的值中?

时间:2017-05-25 07:57:17

标签: javascript jquery html simple-html-dom

$( window ).on( "load", function(){
        var token_val= document.getElementsByName("token")[0].value;

        var real_data = "<input type='hidden' name='token' value='NEED VLAUE OF token_val HERE'>";

        $('form').append(real_data);
});

这是我的代码。我需要将输入标记的值设置为变量token_val的值。

4 个答案:

答案 0 :(得分:2)

使用以下内容更改您的代码

  $( window ).on( "load", function(){
            var token_val= document.getElementsByName("token")[0].value;

            var real_data = "<input type='hidden' name='token' value='"+token_val+"'>";

            $('form').append(real_data);
    });

答案 1 :(得分:2)

使用jQuery的基于对象的方法来创建元素。

$( window ).on( "load", function(){
    var token_val= document.getElementsByName("token")[0].value;

    var real_data = "<input type='hidden' name='token' value='" + token_val + "'>";

    $('form').append(real_data);
});

答案 2 :(得分:1)

其他人已经打败了我的答案,但是如果你喜欢进步并使用ES2015,你可以使用模板字符串:

>>> import logging
>>> logging.basicConfig(
...         format='%(asctime)s %(levelname)-8s %(message)s',
...         level=logging.INFO,
...         datefmt='%Y-%m-%d %H:%M:%S')
>>> 
>>> logging.info('an info messge')
2017-05-25 00:58:28 INFO     an info messge
>>> logging.debug('a debug messag is not shown')
>>> 

答案 3 :(得分:0)

library(shiny)
colA <- c('A','B','C','D','E')
colB <- c(1,2,3,4,5)
rawdata <- as.data.frame(cbind(colA,colB))

ui <- fluidPage(
  mainPanel(
    tabsetPanel(id = "allResults",
                tabPanel(value='inputVars',title='Variable Selection', 
                         verticalLayout(
                           DT::dataTableOutput('inputVars'),
                           br(),
                           fluidRow(align="bottom", 
                                    column(2, actionButton("selectAll"  , strong("Select All"))),
                                    column(3, actionButton("deselectAll", strong("Deselect All")))
                           )
                         )
                ),
                tabPanel(value='temp',title="TEMP", verbatimTextOutput("temp"))
    )
  )
)

server <- function(input, output, session) {

  var <- reactiveValues()
  observeEvent(input$selectAll,{
    print("SelectAll")
    var$selected <- 1:nrow(rawdata)
  })

  observeEvent(input$deselectAll,{
    print("deselectAll")
    var$selected <- 0
  })

  # Default SummaryTable
  output$inputVars <- DT::renderDataTable({
    if (input$selectAll==0 & input$deselectAll==0) {
      print("Default")
      DT::datatable(rawdata, options = list(paging = FALSE, searching = FALSE))
    } 
    else {
      DT::datatable(rawdata, selection = list(target = 'row', selected = var$selected))
    }
  })
}

shinyApp(ui = ui, server = server)