如何在 PHP 的 OnClick 函数中将带有空格的变量作为参数传递

how to pass the variable with space as parameter in onclick function in php

本文关键字:变量 空格 参数传递 PHP OnClick 函数      更新时间:2023-09-26

我想在 onclick 函数中传递带有空格的变量作为参数。这是我的表格值,空格:"文件名示例"

   echo '<input type=checkbox name=".$next."  id=".$next." onclick=my1(this.form,this.checked,".$cid.",'".$vfet['file_name']."','".$id."');>';

脚本代码

echo ('<script>
                function my1(a,ischeck,cid,file,eid)
                {
                alert("welcome");
                 alert("select"+ischeck);
                 alert("file"+file);
                 }
         </script>');

如果值没有空格,则文件值可以很好地传递。

使 html 合法化并在 onclick 值周围加上双引号 - 目前空间使浏览器认为它是一个单独的属性。

你还把单引号和双引号弄混了:

echo '<input type="checkbox" name="'.$next.'"  id="'.$next.'" onclick="my1(this.form,this.checked,'.$cid.','''.$vfet['file_name'].''','''.$id.''');">';

试试

"'.$cid.'"而不是".$cid."或带有"''"