我有以下代码。它为戏剧工作,但真假都回归为“真实”和“虚假”我认为。
var superslider_play = "<?php get_option_tree( 'superslider_play', '', 'false' ); ?>"
var superslider_pagination = "<?php get_option_tree( 'superslider_pagination', '','false' ); ?>"
$('#superslider_home').superslides({
play: superslider_play, // Milliseconds for delay
pagination: superslider_pagination
});
我尝试过使用json_encode()
和.replace("\"","\\\"")
,但似乎没有任何效果。任何帮助将不胜感激。
答案 0 :(得分:4)
您已在<?php ...?>
:
var superslider_pagination = "<?php get_option_tree( 'superslider_pagination', '','false' ); ?>"
// remove the double quotes -^-----------------------------------------------------------------^
假设您希望结果为:
var superslider_pagination = true;
// OR
var superslider_pagination = false;
...当浏览器将其视为JS时。
答案 1 :(得分:1)
如果您想将参数传递为bool(true
或false
),则无需使用引号。
而不是:
<?php get_option_tree( 'superslider_pagination', '','false' ); ?>
试试这个:
<?php get_option_tree( 'superslider_pagination', '',false); ?>
答案 2 :(得分:0)
试试这个<?php echo get_option_tree( 'superslider_play', '', 'false' ); ?>"
你没有返回这个功能。该对象返回的数据类型是什么(进一步提供更多帮助)?
答案 3 :(得分:0)
值为“true”和“false”,因为您在php标记之前和之后使用引号。
以下代码:
var a = <?php echo "hola"; ?> ; //
var b = "<?php echo "hola"; ?>" ; //
输出
var a = hola ;
var b = "hola" ;
关于你使用json_encode的尝试,也许你忘了做回声。
<?php
json_encode(true); // prints nothing
echo json_encode(true); // prints true
?>
在我看来,应该使用json_encode函数。 一般来说,我会选择:
<?php
function get_option_tree($a,$b,$c) {
return true;
}
?>
var superslider_play = <?php echo json_encode(get_option_tree( 'superslider_play', '', 'false' )); ?> ;
var superslider_pagination = <?php echo json_encode(get_option_tree( 'superslider_pagination', '','false' )); ?> ;
$('#superslider_home').superslides({
play: superslider_play, // Milliseconds for delay
pagination: superslider_pagination
});
输出:
var superslider_play = true ;
var superslider_pagination = true ;
$('#superslider_home').superslides({
play: superslider_play, // Milliseconds for delay
pagination: superslider_pagination
});