简体   繁体   English

如何在onclick函数html中传递字符串

[英]how to pass string in onclick function html

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. 我尝试了一个老问题,在那里他们将js中的值传递给js。但是我想将字符串从html传递给javascript。 but it gives 但它给

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> 我如何传递字符串>

If you write test(TEST123) then it looks for the variable with TEST123 name. 如果您编写test(TEST123)则它将查找具有TEST123名称的变量。
so you have to pass it as a string not as a variable 因此您必须将其作为字符串而不是变量传递

 function test(p){ alert(p) } 
 <a href="#" onclick="test('TEST123')"> and 

<a href="#" onclick="test('TEST123')">

只是在您的js函数中写一个字符串参数

If you are using select tag, and want to pass the value on selecting the option item, then you can use 如果您正在使用选择标签,并且希望在选择选项时传递值,则可以使用

<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的内置类。

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

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