![](/img/trans.png)
[英]Why can't I pass a function that returns a string, as a parameter of a function, where the parameter is of type string?
[英]Why can't I pass a string as a parameter in a Javascript function?
我在php中有以下代码:
<?php
$apples = 123123;
echo "<td>
<input type='button' onclick='confirmation($apples)' value='ac/in'>
</td>";
echo('<script type="text/javascript">
function confirmation(test) {
var answer = confirm("Are you sure?")
if (answer){
alert(test);
$.get("/index.php?f=func_name_to_call",
{name: "John", hobby: "Programming"}, function(data) {});
}
}
</script>');
?>
如果$ apples是一个数字,那么参数将被正确传递; 但是当它是一个字符串时,函数confirmation()被破坏。 有任何想法吗? 提前致谢。
如果您希望它将参数作为字符串传递,请使用以下编辑的代码
echo "<td><input type='button' onclick='confirmation(\"$apples\")' value='ac/in'></td>";
只需使用\\
来转义双引号,它将被视为一个字符串。
是的,您必须在调用确认函数中使用转义字符\\“\\”,因为您在整数类型参数中传递整数,该参数没有双引号。因此它将字符串转换为整数...
<?php
$apples = 123123;
?>
<td>
<input type='button' onclick='confirmation(<?php echo $apples?>)' value='ac/in'>
</td>
<script type="text/javascript">
function confirmation(test) {
var answer = confirm("Are you sure?")
if (answer){
alert(test);
$.get("/index.php?f=func_name_to_call",
{name: "John", hobby: "Programming"}, function(data) {});
}
}
</script>
你会遇到麻烦将所有这些'
转换成"
因为你通过php
回应它们,只是一个建议不要echo
它们,使用html
标记,只是逃避你的php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.