在 JavaScript 函数中传递字符串参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31960980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:26:47  来源:igfitidea点击:

Passing string parameter in JavaScript function

javascripthtmleventsbuttononclick

提问by Anurag Chakraborty

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

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

As it's mentioned here- pass string parameter in an onclick 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>

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

但在控制台中,它给出了类似Uncaught SyntaxError: Unexpected token }.

回答by Juned Lanja

Change your code to

将您的代码更改为

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

回答by Nina Scholz

Rename your variable nameto myname, bacause nameis a generic property of windowand is not writable in the same window.

将变量重命名namemyname, bacausename是 的通用属性,window不能在同一窗口中写入。

And replace

并替换

onclick='myfunction(\''" + name + "'\')'

With

onclick='myfunction(myname)'

Working example:

工作示例:

var myname = "Mathew";
document.write('<button id="button" type="button" onclick="myfunction(myname);">click</button>');
function myfunction(name) {
    alert(name);
}

回答by Andy

The question has been answered, but for your future coding reference you might like to consider this.

问题已得到解答,但为了您将来的编码参考,您可能需要考虑这一点。

In your HTML, add the name as an attribute to the button and remove the onclick reference.

在您的 HTML 中,将名称作为属性添加到按钮并删除 onclick 引用。

<button id="button" data-name="Mathew" type="button">click</button>

In your JS, grab the button using its ID, assign the function to the button's clickevent, and using the function to display the button's data-name attribute.

在您的 JS 中,使用其 ID 抓取按钮,将函数分配给按钮的click事件,并使用该函数显示按钮的 data-name 属性。

var button = document.getElementById('button');

button.onclick = myfunction;

function myfunction() {
  var name = this.getAttribute('data-name');
  alert(name);
}

Best of luck.

祝你好运。

DEMO

演示

回答by MJVM

You can pass string parameters to javascript functions like below code:

您可以将字符串参数传递给 javascript 函数,如下面的代码:

I passed 3 parameters where the third one is a string parameter hope this helps.

我传递了 3 个参数,其中第三个是字符串参数,希望这会有所帮助。

var btn ="<input type='button' onclick='RoomIsReadyFunc("+ID+","+RefId+",\""+YourString+"\");'  value='Room is Ready' />";

//your javascript function

function RoomIsReadyFunc(ID, RefId, YourString)
{
     alert(ID);
     alert(RefId);
     alert(YourString);
}

回答by banoth ravinder

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

Better to use `` than "", This is more dynamic answer.

最好使用``而不是“”,这是更动态的答案。

回答by Sameer

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