duandu6497
2014-12-02 21:08
浏览 416
已采纳

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

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.

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.)

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 onClick function is just onClick="trackIt()", then it works fine and I can alert "Hi!" by removing the parameter.

Thanks for the help! =]

图片转代码服务由CSDN问答提供 功能建议

我查看了很多类似的例子,但我无法做到这一点。 < p>所以,我有这个:

  echo('&lt; td colspan =“3”style =“background-color:#005673; text-align:right; padding  :4px 0px;“&gt; 
&lt; button class =”btnTrack“onClick =”trackIt('。$ name。')“&gt; Track It!&lt; / button&gt;&lt; / td&gt;'); 
    
 
 

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

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

然后,我有一个简单的脚本 (只是看它是否会起作用):

 &lt; script&gt; 
 //点击Track It 
 
function trackIt(param)
 {\  n alert(“嗨!”); 
 alert(param); 
} 
 
&lt; / script&gt; 
   
 
 

但是,它不起作用 。

如果我的 onClick 函数只是 onClick =“track 它()“,然后它工作正常,我可以提醒”嗨!“ 删除参数。

感谢您的帮助! =]

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongxing2692 2014-12-02 21:10
    已采纳

    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,

    <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..

    <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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doumeng4400 2014-12-02 21:15

    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:

    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

    评论
    解决 无用
    打赏 举报
  • douying8666 2014-12-02 21:17

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

    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 :)

    评论
    解决 无用
    打赏 举报
  • drpqxogph15436713 2017-08-21 19:10

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

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

    This works for me.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题