【问题标题】:Passing string parameter in JavaScript function在 JavaScript 函数中传递字符串参数
【发布时间】:2015-11-04 19:30:06
【问题描述】:

我试图将字符串传递给 JavaScript 函数。

正如这里提到的 - Pass a string parameter in an onclick function

我正在使用这个简单的代码-

<!DOCTYPE html>
<html>

    <body>
        <script>
            name = "Mathew";
            document.write("<button id='button' type='button' onclick='myfunction(\''" + name + "'\')'>click</button>")

            function myfunction(name)
            {
                alert(name);
            }
        </script>
    </body>

</html>

但在控制台中它会给出类似Uncaught SyntaxError: Unexpected token } 的错误。

【问题讨论】:

  • 您尝试将表格单元格添加到没有表格的页面的任何原因?
  • 请注意,该问题的最佳答案还推荐了适当的 DOM 方法,您可能需要考虑这些方法 - 它可以防止出现此类问题。

标签: javascript html events button onclick


【解决方案1】:

把你的代码改成

document.write("<td width='74'><button id='button' type='button' onclick='myfunction(\""+ name + "\")'>click</button></td>")

【讨论】:

    【解决方案2】:

    将变量name 重命名为myname,因为name 是window 的通用属性,不能在同一窗口中写入。

    并替换

    onclick='myfunction(\''" + name + "'\')'
    

    有

    onclick='myfunction(myname)'
    

    工作示例:

    var myname = "Mathew";
    document.write('<button id="button" type="button" onclick="myfunction(myname);">click</button>');
    function myfunction(name) {
        alert(name);
    }

    【讨论】:

    • 如果我使用此代码,它不会在警告框中打印任何内容。
    【解决方案3】:

    问题已得到解答,但对于您未来的编码参考,您可能需要考虑这一点。

    在您的 HTML 中,将名称作为属性添加到按钮并删除 onclick 引用。

    <button id="button" data-name="Mathew" type="button">click</button>
    

    在您的 JavaScript 中,使用按钮的 ID 获取按钮,将函数分配给按钮的 click 事件,然后使用该函数显示按钮的 data-name 属性。

    var button = document.getElementById('button');
    
    button.onclick = myfunction;
    
    function myfunction() {
      var name = this.getAttribute('data-name');
      alert(name);
    }
    

    DEMO

    【讨论】:

      【解决方案4】:
      document.write(`<td width='74'><button id='button' type='button' onclick='myfunction(\``+ name + `\`)'>click</button></td>`)
      

      使用 `` 比使用 "" 更好。这是一个更动态的答案。

      【讨论】:

        【解决方案5】:

        您可以将字符串参数传递给 JavaScript 函数,如下代码:

        我传递了三个参数,其中第三个是字符串参数。

        var btn ="<input type='button' onclick='RoomIsReadyFunc(" + ID + "," + RefId + ",\"" + YourString + "\");'  value='Room is Ready' />";
        
        // Your JavaScript function
        
        function RoomIsReadyFunc(ID, RefId, YourString)
        {
            alert(ID);
            alert(RefId);
            alert(YourString);
        }
        

        【讨论】:

          【解决方案6】:

          使用这个:

          document.write('<td width="74"><button id="button" type="button" onclick="myfunction('" + name + "')">click</button></td>')
          

          【讨论】:

          • document.write('')
          【解决方案7】:

          试试这个...

          onclick="myfunction('name')";

          【讨论】:

            猜你喜欢
            • 2012-03-27
            • 1970-01-01
            • 2011-12-13
            • 2011-05-12
            • 1970-01-01
            • 2017-02-27
            • 2020-01-01
            • 1970-01-01
            • 2015-01-02
            相关资源
            最近更新 更多