当我在php代码中回应时,javascript功能不会执行 [英] a javascript fuction doesn't execute when i echo that in php code
本文介绍了当我在php代码中回应时,javascript功能不会执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的页面上回显以下的html代码。但是当我这样做时, onclick
事件不起作用。 $ senduser
具有正确的值
$ table1 =< div id ='sendmsg'class ='row'>
< div class ='col-lg-6'>
< textarea id ='msg'cols = '40'placeholder = '在这里写信息...'>< / textarea>
< / div>< div class ='col-lg-2'>
< button type ='button 'onClick ='sendmsg(。$ senduser。)'class ='btn btn-success'value ='send'id ='msgbtn'>发送到< / button>< / div>< / div> ;
echo $ table1;
我的javascript功能:
函数sendmsg(senduser){
alert(senduser);
。
。
。
}
Alert不会显示任何内容。
<解决方案 $ senduser
可能是一个字符串,因为你不引用它,它被解释为一个( undefined ...)变量。
您需要引用它:
...
// ^^这里^^和这里
I want to echo the following html code on my page. But the onclick
event doesn't work when I do this. the $senduser
has the correct value
$table1="<div id='sendmsg' class='row'>
<div class='col-lg-6'>
<textarea id='msg' cols='40' placeholder='Write your message here...'></textarea>
</div><div class='col-lg-2' >
<button type='button' onClick='sendmsg(".$senduser.")' class='btn btn-success' value='send' id='msgbtn'>Send to</button></div></div>";
echo $table1;
my javascript function:
function sendmsg(senduser){
alert(senduser);
.
.
.
}
Alert doesn't show anything.
解决方案
$senduser
is probably a string and as you don't quote it, it is interpreted as a (undefined...) variable in javascript.
You need to quote it:
...
<button type='button' onClick='sendmsg(\"".$senduser."\")' class='btn btn-success' ...
// ^^ here ^^ and here
这篇关于当我在php代码中回应时,javascript功能不会执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文