[英]javascript code inside php echo
这是我在HTML中的功能onClick脚本,它可以完美运行:
<input type="text" id="t" name="t" value="1">
<a href="testar.php?action=" onclick="window.location=this.href+'&quant='+document.getElementById('t').value;return false;">Click</a>
我想要实现的是:
echo '<input type="text" value="1" id="t" name=""/>
<a href="addCarrinho.php?id_produto='.$registo['id'].'&nome='.$registo['nome'].'&preco='.$registo['preco'].'" onclick="window.location=this.href+'&quant='+document.getElementById('t').value;return false;">Add</a>';
所有的php作品,我在另一页上收到php变量,但onClick javaScript不能正常工作,即时通讯收到一个sintaxe错误。 我认为这是一个引号问题,但我不熟悉javaScript。
更改为以下内容:
<a href="addCarrinho.php?id_produto='.$registo['id'].'&nome='.$registo['nome'].'&preco='.$registo['preco'].'" onclick="window.location=this.href+\'&quant=\'+document.getElementById(\'t\').value;return false;">Add</a>';
您需要对最后一行的单引号进行转义,如下所示:
...
onclick="window.location=this.href+\'&quant=\'+document.getElementById(\'t\').value;return false;"
...
echo '<input type="text" value="1" id="t" name=""/>
<a href="addCarrinho.php?id_produto='.$registo["id"].'&nome='.$registo["nome"].'&preco='.$registo["preco"].'" onclick="window.location=this.href+'&quant='+document.getElementById("t").value;return false;">Add</a>';
用这种方式尝试,我做了一些单引号和双引号更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.