javascript 使用 HTML onClick 将 PHP 变量作为参数传递

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

Passing a PHP variable as a parameter using HTML onClick

javascriptphphtmlonclick

提问by Felipe

I looked through many similar examples, but I could not make this work.

我查看了许多类似的例子,但我无法完成这项工作。

So, I have this:

所以,我有这个:

echo('<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;">
      <button class="btnTrack" onClick="trackIt(' . $name . ')" >Track It!</button></td>');

That code is inside a for, so the variable $name will be different depending where I am at.

该代码在 for 中,因此变量 $name 将根据我所在的位置而有所不同。

I am not trying to make things complicates, so first, I am just trying to pass that parameter to the function trackIt (I actually need to pass 2 of them.)

我不想让事情变得复杂,所以首先,我只是想将该参数传递给函数 trackIt(我实际上需要传递其中的 2 个。)

Then, I have a simple script (just to see if it will work):

然后,我有一个简单的脚本(只是为了看看它是否有效):

<script>
//After you click on Track It

function trackIt(param) 
{
   alert("Hi!");
   alert(param);
}

</script>

However, it does not work.

但是,它不起作用。

If my onClickfunction is just onClick="trackIt()", then it works fine and I can alert "Hi!" by removing the parameter.

如果我的onClick函数只是onClick="trackIt()",那么它工作正常,我可以提醒“嗨!” 通过删除参数。

Thanks for the help! =]

谢谢您的帮助!=]

回答by Sampath Liyanage

Try this..

试试这个..

echo('<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;">
  <button class="btnTrack" onclick="trackIt(\'' . $name . '\')" >Track It!</button></td>');

Note: you have to use your way if the input parameter is a numeric value..

注意:如果输入参数是数值,则必须使用您的方式。



You are printing HTML as,

您正在打印 HTML,

<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;">
<button class="btnTrack" onclick="trackIt(test)" >Track It!</button></td>

But because the function trackIt needs a string as the input parameter, you have to print this..

但是因为函数trackIt需要一个字符串作为输入参数,所以你必须打印这个..

<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;">
<button class="btnTrack" onclick="trackIt('test')" >Track It!</button></td>

As you are using 'as boundaries to define strings in PHP, you have to escape it using \'in order to make 'character part of the string.

当您'在 PHP 中用作定义字符串的边界时,您必须使用它来转义它\'以使'字符成为字符串的一部分。

回答by Ohgodwhy

The reason this doesn't work is due to the fact that you're passing an unencapsulated string as an argument in your HTML, however, it's being interpreted as a javascript variable, such as:

这不起作用的原因是因为您将未封装的字符串作为参数传递到 HTML 中,但是,它被解释为 javascript 变量,例如:

Uncaught ReferenceError: ValueOf$name is not defined

' Or if the variable contains spaces or special characters:

' 或者如果变量包含空格或特殊字符:

 Uncaught SyntaxError: Unexpected identifier

To resolve this, you should encapsulate your string with quotations.

要解决此问题,您应该用引号将字符串封装起来。

"trackIt(\'' . $name . '\')"

If you're using an integer, string encapsulation is not required.

如果您使用的是整数,则不需要字符串封装。

jsFiddle examples

jsFiddle 示例

回答by Mr Talha

For passing multiple parameters you can cast the string by concatenating it with the ASCII value like, for single quotes we can use &#39

要传递多个参数,您可以通过将字符串与 ASCII 值连接来转换字符串,例如,对于单引号,我们可以使用 &#39

var str= "&#39;"+ str+ "&#39;";

This works for me.

这对我有用。

回答by Marco Mura

Even if other answers are true... If you do not need to really print all that html use php differently

即使其他答案是正确的...如果您不需要真正打印所有 html,请以不同的方式使用 php

echo('<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;">
      <button class="btnTrack" onClick="trackIt(' . $name . ')" >Track It!</button></td>');

it can become:

它可以变成:

<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;">
          <button class="btnTrack" onClick="trackIt(<?php print $name;?>)" >Track It!</button></td>

Use php only when needed :)

仅在需要时使用 php :)