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

前端 未结 8 1588
长情又很酷
长情又很酷 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:35

    When adding attributes to an html element, you are already within the scope of pug, so you can just use pug variables like regular js variables.

    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:35

    I came across a similar issues and solved it rather differently (by escaping params). In my case, I needed to pass the following template values to a javascript function as argument when a button is clicked

    {
      url:"http://google.com",
      token: "Bearer your-token", 
      accountId: "abc123"
    }
    

    So the pug in my case looked as follow

    button(onclick='authenticate(\'' + url + '\',\'' + token + '\',\'' + accountId + '\')') Login
    

    And the resulting html is as follow

    <button onclick="authenticate('http://google.com','Bearer your-token','abc123')">Login</button>
    
    0 讨论(0)
提交回复
热议问题