简体   繁体   English

如何将值传递给(Jade)pug 中的 onclick 函数?

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

I am new to jade and stuck on this issue.我是 jade 的新手,一直在解决这个问题。 I think I have tried everything from the StackOverflow posts and still at nothing.我想我已经尝试了 StackOverflow 帖子中的所有内容,但仍然一无所获。

The things I have tried我尝试过的事情

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

Error错误

1:8 Uncaught SyntaxError: Invalid or unexpected token

Changing it to !{val.link}将其更改为!{val.link}

Error错误

Uncaught SyntaxError: Unexpected token .

Changing it to "!{val.link}" and "#{val.link}" just gives me string understandably so.将其更改为"!{val.link}""#{val.link}"只是给了我可以理解的字符串。 BTW val.link is a string顺便说一句 val.link 是一个字符串

Just giving val.link says Uncaught ReferenceError: val is not defined只是给 val.link 说Uncaught ReferenceError: val is not defined

I am out of options now.我现在别无选择。 Help will be appreciated.帮助将不胜感激。

Thanks谢谢

当给一个 html 元素添加属性时,你已经在 pug 的范围内,所以你可以像使用常规 js 变量一样使用 pug 变量。

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

我只是使用了下面的代码,它对我有用(带有 pre 和 pos 引号)

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

You just need to put onclick="myfunction(#{varible.atributo})"你只需要把 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

Use differing nested quotation marks so that you pass a string to your gotoBlog function.使用不同的嵌套引号,以便将字符串传递给 gotoBlog 函数。 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}')" )

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} )`)

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就我而言,当单击按钮时,我需要将以下template values作为参数传递给 javascript 函数

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

So the pug in my case looked as follow所以我的情况下的pug看起来如下

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

And the resulting html is as follow生成的 html 如下

<button onclick="authenticate('http://google.com','Bearer your-token','abc123')">Login</button>

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.注意:外部/内部/所有引号可以是' (单)或" (双)引号,我使用单引号和双引号来提高可读性。

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

This worked for me to use values that I passed to pug inside an onClick function.这对我有用,可以使用我在 onClick 函数中传递给 pug 的值。

Too late but most of the options above didn't work for me but this did为时已晚,但上面的大多数选项对我都不起作用,但确实如此

button(onclick='gotoBlog('+'"'+val.link+'"'+')')

or simpler:或更简单:

onclick=`gotoBlog('${val.link}')`

Basically converts val.link to a string.基本上将 val.link 转换为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM