php echo里面的javascript代码
问题描述:
This is my functional onClick script in HTML and it works perfectly:
<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>
What i want to achieve is this:
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>';
All php works, i am receiving php variables on the other page but the onClick javaScript is not working, im getting a sintaxe error. I think its a quotes problem but im not familiarized with javaScript.
这是我在HTML中的功能onClick脚本,它运行良好: p>
&lt; input type =“text”id =“t”name =“t”value =“1”&gt;
&lt; a href =“testar.php?action =”onclick =“window.location = this .href +'&amp; quant ='+ document.getElementById('t')。value; return false;“&gt; Click&lt; / a&gt;
code> pre>
我是什么 希望实现的是: p>
echo'&lt; input type =“text”value =“1”id =“t”name =“”/&gt;
&lt; a href =“addCarrinho.php?id_produto ='。$ registo ['id']。'&amp; nome ='。$ registo ['nome']。'&amp; preco ='。$ registo ['preco']。 '“onclick =”window.location = this.href +'&amp; quant ='+ document.getElementById('t')。value; return false;“&gt; Add&lt; / a&gt;';
code>
所有的php工作,我在另一个页面上接收php变量,但onClick javaScript不工作,我得到一个sintaxe错误。
我认为它是一个引号问题,但我不熟悉javaScript 。 p>
div>
答
Change to the following:
<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>';
答
You need to escape the single quotes in the last line like following:
...
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>';
Try with this way, I did some single and double quote change.