How to pass value to a onclick function in (Jade)pug?

前端 未结 8 1587
长情又很酷
长情又很酷 2021-01-02 10:54

I am new to jade and stuck on this issue. I think I have tried everything from the StackOverflow posts and still at nothing.

The things I have tried



        
相关标签:
8条回答
  • 2021-01-02 11:12

    You just need to put onclick="myfunction(#{varible.atributo})"

    Here a example:

    table
    thead
        tr
            th #ID
            th Description
            th Actions
    tbody
        each item, i in itemlist
            tr
                th(scope='row') #{item.id}
                td #{item.description}
                td
                    button(onclick="editItem(#{item.id})", title="Edit")
                        |  Edit
    
    0 讨论(0)
  • 2021-01-02 11:14

    Use differing nested quotation marks so that you pass a string to your gotoBlog function. Here, I use single ticks within double ticks.

    button(type='button' class=' c-btn-blue c-btn-circle c-btn-uppercase' value="Read More" onclick="gotoBlog( '#{val.link}' )")
    

    In other words:

    button( onclick= "myFunction('#{stringVariable}')" )
    
    0 讨论(0)
  • 2021-01-02 11:17

    Too late, I know :(

    But, this could be useful!

    `${}`
    

    So the code will be

    button(type='button' class=' c-btn-blue c-btn-circle c-btn-uppercase' value="Read More" onclick=`gotoBlog( ${val.link} )`)
    
    0 讨论(0)
  • 2021-01-02 11:31

    I just used the code below and it worked for me (with pre and pos quotes)

    button(type='button', onclick='someFunction("'+ yourObject.id +'")' ) PressMe

    0 讨论(0)
  • 2021-01-02 11:31

    When using multiple parameters in a function, this did the trick:

    'myFunction(' + '"' + varA + '"' + ',' + '"' + varB + '"' + ')'

    NOTE: outer/inner/all quote marks can be ' (single) or " (double) quote marks, I used single and double quote marks for readability.

    0 讨论(0)
  • 2021-01-02 11:32

    button(type='button', onClick='function(\'' + someValue + '\')') Text

    This worked for me to use values that I passed to pug inside an onClick function.

    0 讨论(0)
提交回复
热议问题