php echo里面的javascript代码

时间:2015-09-09 04:00:18

标签: javascript php html

这是我在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。

3 个答案:

答案 0 :(得分:2)

更改为以下内容:

<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>';

答案 1 :(得分:1)

您需要在最后一行中转义单引号,如下所示:

...
onclick="window.location=this.href+\'&quant=\'+document.getElementById(\'t\').value;return false;"
...

答案 2 :(得分:0)

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>';

尝试这种方式,我做了一些单引号和双引号更改。