【问题标题】:PHP dynamic button onclick dosen't workPHP动态按钮onclick不起作用
【发布时间】:2016-01-22 16:54:54
【问题描述】:

我创建了一个用于创建 html 输入的类,即 createButton()。下面一个是用于创建可选参数的类,如 onclick、style、class 等。我尝试传递 style、class 和 onclick 参数。但由于字符串排列,onclick 不起作用。单引号和双引号都让我感到困惑。

 $cmsform->setOption("style='margin-left: 10px !important;' class='submit' onclick='return confirm('Are you sure you want to delete this staff?')");

【问题讨论】:

    标签: php function class parameters onclick


    【解决方案1】:
     $cmsform->setOption("style='margin-left: 10px !important;' class='submit' onclick='return confirm(\'Are you sure you want to delete this staff?\')");
    

    试试吧。

    【讨论】:

      【解决方案2】:
      onclick='return confirm('Are you sure you want to delete this staff?')
      

      不会像 html 本身那样工作。

      1.添加单引号关闭onclick

      onclick='return confirm('Are you sure you want to delete this staff?')'
      

      2.转义单引号:

      onclick='return confirm(\'Are you sure you want to delete this staff?\')'
      

      3.转义反斜杠,因为您在 php 中使用双引号

      onclick='return confirm(\\'Are you sure you want to delete this staff?\\')'
      

      所以:

      $cmsform->setOption("onclick='return confirm(\\'Are you sure you want to delete this staff?\\')'");
      

      【讨论】:

        【解决方案3】:

        错误是因为这个。

          onclick='return confirm('Are you sure you want to delete this staff?')");
        

        如果你感到困惑,你可以像这样逃避特殊字符。

          onclick='return confirm(\'Are you sure you want to delete this staff?\')'");
        

        使用 \ 转义特殊字符。

        希望对您有所帮助!

        【讨论】:

          【解决方案4】:
          onclick='return confirm('Are you sure you want to delete this staff?')
          

          它破坏了你的代码,

          onclick='return confirm(' 
          

          onclick 代码仅在 (' 之前有效,因为它在 ' 上显示终止。您可以使用 (\' 以获得更好的表示和工作。

          完整的代码行:

           $cmsform->setOption("style='margin-left: 10px !important;' class='submit' onclick='return confirm(\'Are you sure you want to delete this staff?\')");
          

          希望这能解决您的问题。

          【讨论】:

          • 我知道。那是你们在这里为你们提供建议,来解决这个问题。
          • 如果这有帮助,您也可以将答案标记为正确并点赞,这样其他人也可以轻松识别问题。
          【解决方案5】:

          双引号用于php。 onclick=' 中的单引号用于 html。 confirm(' 中的单引号用于 JavaScript。

          这里的问题是 JavaScript 的单引号与 html 的单引号冲突。您可以将其中一个更改为双引号,但它会与 php.ini 冲突。所以你必须转义一组引号。你逃脱的人取决于你。这是一种可能的解决方案。

          $cmsform->setOption("style='margin-left: 10px !important;' class='submit' onclick='return confirm(\"Are you sure you want to delete this staff?\")'");
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-07-10
            • 2018-05-10
            • 1970-01-01
            • 2012-03-26
            • 2015-02-24
            • 2017-10-23
            • 1970-01-01
            相关资源
            最近更新 更多