[英]Passing php variable argument to onclink function through python script
我一直在尝试调用一个onclick js函数,该函数包含一个PHP变量形式的参数。 我的代码如下所示:
echo "<input type = 'button' onclick = 'activateit(".json_encode($name).")' value = '$name'>";
但是它给出了以下错误:
activateit is not defined at HTMLInputElement.onclick
chrome inspect元素显示如下:
onclick = 'activateit("chai")'
其中$ name = chai。 我已经为此停留了很长时间。 请帮忙。
如注释中所述,使用以下命令可以使事情变得更好,您可以将值分配给id或要调用它的任何属性,对于添加的每个按钮,您都可以使用class属性,这样您可以添加多个按钮而不会出现任何问题,并且避免有很多点击事件声明。 当然,您还必须包括jquery脚本标签
// echo "<input type = 'button' onclick = 'activateit(".json_encode($name).")' value = '$name'>"; is change to the below
echo "<input type = 'button' id=".json_encode($name)." value = '$name' class='added_button'>";
然后使用$ (document).on
因为我们希望针对任何存在的按钮以及页面加载后是否添加了更多按钮来触发事件
$ (document).on ('click', '.added_button', function (e) {
// this will be added if you dont want a form to submit when the button is pressed if it is in a form
// or to stop it from doing whatever it was meant to do
e.preventDefault ();
var name = this.id; // this will give you the value 'chai'
// to test it we do an alert
alert (name);
// add your code here
});
包括jquery使用<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.