在JavaScript函数中传递字符串参数 [英] Passing string parameter in JavaScript function

查看:253
本文介绍了在JavaScript函数中传递字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将字符串传递给javascript函数.

I was trying to pass a string to a javascript function.

此处已提及-在onclick函数中传递字符串参数

我正在使用此简单代码-

I'm using this simple code-

<!DOCTYPE html>
<html>
<body>    
<script>
name = "Mathew";
document.write("<button id='button' type='button' onclick='myfunction(\''" + name + "'\')'>click</button>")

function myfunction(name)
{
alert(name);
}
</script>
</body>
</html>

但是在控制台中,它会显示类似Uncaught SyntaxError: Unexpected token }的错误.

But in console it's giving error like Uncaught SyntaxError: Unexpected token }.

推荐答案

将代码更改为

document.write("<td width='74'><button id='button' type='button' onclick='myfunction(\""+ name + "\")'>click</button></td>")

这篇关于在JavaScript函数中传递字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆