javascript 无法将字符串传递给函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19506220/
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-10-27 15:49:20  来源:igfitidea点击:

cannot pass string to function

javascriptjqueryfunction

提问by user2860857

I'm using javascript and I try to pass a string to a function , like so

我正在使用 javascript 并尝试将字符串传递给函数,就像这样

        //example string
        var f="a";

//add button that sends the value of f to the function
     document.getElementById("mydiv").innerHTML="<input type='button' id='myButton' value='Click here' onclick='gothere("+f+");'> ";

    function gothere(a){
    alert(a);
    }

I never see the alert and in console I see a is not defined(refers to the f I guess?)

我从来没有看到警报,在控制台中我看到了a is not defined(指的是我猜的 f?)

If i set the f varto be a number then I see the alert.

如果我将 fvar设置为一个数字,那么我会看到警报。

What am I missing?

我错过了什么?

Thanks in advance

提前致谢

EDIT

编辑

I was thinking maybe something like

我在想也许像

var buttonnode= document.createElement('input');

document.getElementById("mydiv").appendChild(buttonnode);
buttonnode.onclick=gothere(f);

Wont work for the same reason?

不会因为同样的原因工作吗?

采纳答案by CDspace

When your HTML get's rendered, you get onclick='gothere(a);', but the actual avariable doesn't exist in this context, you want to pass the value of f, as a string, so you'll need to use onclick='gothere(\""+f+"\");'. Note the extra quotes inside the parens. This will render to onclick='gothere("a");'thus passing the string.

当您的 HTML get 被呈现时,您会得到onclick='gothere(a);',但实际a变量在此上下文中不存在,您希望将 f 的值作为字符串传递,因此您需要使用onclick='gothere(\""+f+"\");'. 注意括号内的额外引号。这将呈现onclick='gothere("a");'从而传递字符串。

When using a number, it works, because calling onclick='gothere(5);'is valid, since a variable can't be named 5, and it passes the number.

使用数字时,它有效,因为调用onclick='gothere(5);'是有效的,因为变量不能被命名5,并且它传递了数字。

回答by Afzaal Ahmad Zeeshan

Actually, you don't have an ain your code. You are using variable fto denote a. So using this would help you:

实际上,您a的代码中没有。您正在使用变量f来表示a. 因此,使用它可以帮助您:

var f="a";
// write the remains of the code as they are..

function gothere(f) {
  alert(f);
}

Now when you'll call the function, there will be an alert of ain the browser.

现在,当您调用该函数时,a浏览器中将出现警告。

Also, try wrapping the content in ""double qoutes to let the code understand that this is a string not a character.

此外,尝试将内容包装在""双引号中,让代码理解这是一个字符串而不是字符。

For onclick use

供点击使用

onclick='gothere(" + f + ")'

And now, its onto you to write the value. Maybe the issue is because you're not writing the value for the f.

现在,由您来写值。也许问题是因为您没有为f.

Try inpecting the error. I am sure there won't be anything.

尝试检查错误。我相信不会有什么。

Or try using the attribute field and change it using jQuery.

或者尝试使用属性字段并使用jQuery.

回答by Nis

How about fixing your code ? You are missing the quotes around the value denoted by variable F. Hence, when variable F is parsed, the function becomes gothere(a). while ais not a defined variable (but its a value) and hence the error.

如何修复你的代码?您缺少变量 F 表示的值周围的引号。因此,解析变量 F 时,函数变为gothere(a)。whilea不是定义的变量(但它是一个值),因此是错误。

Try this !

试试这个 !

document.getElementById("mydiv").innerHTML="<input type='button' id='myButton' value='Click here' onclick='gothere(\""+f+"\");'> ";

The modified part is onclick='gothere(\""+f+"\");'> "

修改后的部分是 onclick='gothere(\""+f+"\");'> "

This should work for you !

这应该适合你!

回答by KAUSHAL J. SATHWARA

function parameter string value image dynamically from JSON. Since item.product_image2 is a URL string, you need to put it in quotes when you call changeImage inside parameter.

从 JSON 动态获取函数参数字符串值图像。由于 item.product_image2 是一个 URL 字符串,因此在调用 changeImage 参数时需要将其放在引号中。

My Function Onclick inside pass parameter.

我的函数 Onclick 内部传递参数。

items+='<img src='+item.product_image1+' id="saleDetailDivGetImg">';
items+="<img src="+item.product_image2+"  onclick='changeImage(\""+item.product_image2+"\");'>";

My Function

我的功能

<script type="text/javascript">
function changeImage(img)
 {
    document.getElementById("saleDetailDivGetImg").src=img;
    alert(img);
}
</script>

回答by alex025

  1. You need to use single quotation marks for value arguments (see @Nis or @CDspace answer).
  2. Better way to handling dynamic clicks or other events is event binding. See jQuery event binding for example.
  1. 您需要对值参数使用单引号(请参阅 @Nis 或 @CDspace 答案)。
  2. 处理动态点击或其他事件的更好方法是事件绑定。例如,请参阅 jQuery 事件绑定。