2015-09-09 67 views
0

这是在HTML我的功能的onClick脚本,它完美的作品:的JavaScript里面的PHP代码回声

<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。

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

谢谢Mr.B工作完美:) –

1

你一定要逃逸的最后一行的单引号类似以下内容:

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

这工作太谢谢你:) –

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

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

+0

甚至没有运行,但谢谢你反正。 –