Javascript 如何在onclick函数html中传递字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35792954/
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
how to pass string in onclick function html
提问by pc_coder
I tried with old question, there they passing the value in js to js.But I would like to pass string from html to javascript. but it gives
我尝试过旧问题,他们将 js 中的值传递给 js。但我想将字符串从 html 传递给 javascript。但它给
Uncaught reference error
未捕获的引用错误
my html
is
我的html
是
<a href="#" onclick="test(TEST123)">
and my javascript
is,
而我的javascript
是,
function test(p){
alert(p)
}
how can I pass the string>
我如何传递字符串>
回答by Divyesh Savaliya
If you write test(TEST123)
then it looks for the variable with TEST123
name.
so you have to pass it as a string not as a variable
如果你写,test(TEST123)
那么它会查找带有TEST123
名称的变量。
所以你必须将它作为字符串而不是变量传递
function test(p){
alert(p)
}
<a href="#" onclick="test('TEST123')"> and
回答by pc_coder
<a href="#" onclick="test('TEST123')">
just wite string parameter in your js function
在你的 js 函数中使用 wite 字符串参数
回答by Sharjeel Rehman
If you are using select tag, and want to pass the value on selecting the option item, then you can use
如果您使用的是 select 标签,并希望在选择选项项时传递值,那么您可以使用
<select class="form-control" onchange="checkifclick('hello')" name="product_name">
and in javascript,
在 JavaScript 中,
<script>
function checkifclick(test) {
alert(test);
}
Basically, i had faced this problem once, i wanted to call the function and wanted to pass the variables into that javascript function.
基本上,我曾经遇到过这个问题,我想调用该函数并将变量传递给该 javascript 函数。
class="form-control"
is using to beautify the element, form-control
is a build-in class of bootstrap.
class="form-control"
用于美化元素,form-control
是bootstrap的内置类。