我在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()被破坏。有任何想法吗?提前谢谢。
答案 0 :(得分:7)
如果您希望将参数作为字符串传递,请使用以下编辑的代码
echo "<td><input type='button' onclick='confirmation(\"$apples\")' value='ac/in'></td>";
只需使用\
来转义双引号,它就会被视为字符串。
答案 1 :(得分:2)
是的,你必须在调用确认函数中使用转义字符\“\”,因为你在一个没有双引号的整数类型参数中传递整数。因此它将字符串转换为整数......
答案 2 :(得分:1)
<?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
它们,使用{ {1}}标记,然后转义html