如何从 PHP 打印按钮调用 JavaScript 函数

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

How to call JavaScript function from a PHP printed button

phpjavascript

提问by imulsion

What a weird question. If anyone has a better title for this, please go ahead and edit it; I can't think of what else to call it. I am printing a button from PHP using echo. The purpose of a button is to show a form by calling a JavaScript function that uses document.getElementById()to change the style attribute of the form tag to visible so the form is visible to the user. The problem I'm having is that the echoed string has to have quotes around it, the onclickevent has to have quotes around it, and the parameter passed to the JavaScript function has to have quotes around it. I tried escpaing the quotes for the parameter with backslashes but that didn't work. Can anyone help me?

多么奇怪的问题。如果有人对此有更好的标题,请继续编辑;我想不出还能叫它什么。我正在使用echo. 按钮的目的是通过调用 JavaScript 函数来显示表单,该函数用于document.getElementById()将表单标签的样式属性更改为可见,以便用户可以看到表单。我遇到的问题是回显的字符串必须有引号,onclick事件必须有引号,传递给 JavaScript 函数的参数必须有引号。我尝试用反斜杠转义参数的引号,但这没有用。谁能帮我?

Here is my code:

这是我的代码:

echo "<input type = 'button' onclick = 'showform(\'psswdform\')' value = 'Change password'>";//this shows the form and needs quotes

JavaScript function:

JavaScript 函数:

function showform(id)
{
document.getElementById(id).style.visibility = "visible";
}

回答by Daan Timmer

This works fine for me? (working example)

这对我有用吗?(工作示例)

<?php

echo "<input type = 'button' onclick = 'showform(\"psswdform\")' value = 'Change password'>";//this shows the form and needs quotes

Generates:

产生:

<input type = 'button' onclick = 'showform("psswdform")' value = 'Change password'>

回答by if __name__ is None

Try this:

试试这个:

echo "<input type = 'button' onclick = 'showform('psswdform')' value = 'Change password'>";

Additionally, you could try jQuery, a super popular JS Library that makes life easier.

此外,您可以尝试使用 jQuery,这是一个超级流行的 JS 库,可以让生活更轻松。

I would make the following CSS class.

我会制作以下 CSS 类。

.hide {
   display:none;
}

Than I would add the following jQuery:

比我会添加以下 jQuery:

$("#show-password-reset").click(function(event){
 event.preventDefault();
 $("#passwdform").show();
});

And finally include your show button:

最后包括你的显示按钮:

<input id="show-password-reset" type = 'button' value = 'Change password'>

Make sure to use class="hide"on the passwdformelement so its hidden when page loads.

确保class="hide"passwdform元素上使用,以便在页面加载时隐藏。

回答by AwesomeBobX64

Try concatenating the string using a combination of single and double quotes like this:

尝试使用单引号和双引号的组合连接字符串,如下所示:

echo '<input type="button" onclick="showform(' . "'psswdform'" . ')" value="Change password">';

echo '<input type="button" onclick="showform(' . "'psswdform'" . ')" value="Change password">';

回答by KillerX

Inside PHP \ is already an escape character. If you want to output "\" you need to echo "\\".

在 PHP 中 \ 已经是一个转义字符。如果要输出“\”,则需要回显“\\”。