如何将 PHP 变量值转换为布尔值并在 jquery 设置中使用

我正在尝试从选项面板传递值以在 jQuery 设置值上使用它们 一切正常,除了true或false SO 如何转换它或以正确的方式使用它。


我正在尝试$offers_animation_type = true 它返回true但不像直接写入true或false在选项值中那样工作。


$("#Ticker").breakingNews({

    effect      :"slide-v",

    autoplay    :<?php echo $offers_animation_type;?>,

    timer       :<?php echo $offers_animation_change;?>,

    color       :"turquoise",

    border      :true

});

但它应该像


$("#Ticker").breakingNews({

    effect      :"slide-v",

    autoplay    :true,

    timer       :3000,

    color       :"turquoise",

    border      :true

});


慕容森
浏览 116回答 3
3回答

白板的微信

在 PHP 中打印true将导致 1,但打印false不会打印任何内容。所以当值为 时false,你破坏了 JS 代码。( autoplay:,)$("#Ticker").breakingNews({&nbsp; &nbsp; effect&nbsp; &nbsp; &nbsp; :"slide-v",&nbsp; &nbsp; autoplay&nbsp; &nbsp; :<?php echo $offers_animation_type ? "true" : "false";?>,&nbsp; &nbsp; timer&nbsp; &nbsp; &nbsp; &nbsp;:<?php echo $offers_animation_change;?>,&nbsp; &nbsp; color&nbsp; &nbsp; &nbsp; &nbsp;:"turquoise",&nbsp; &nbsp; border&nbsp; &nbsp; &nbsp; :true});

jeck猫

您可以像这样在 php 中使用三元运算符&nbsp; &nbsp; $("#Ticker").breakingNews({&nbsp; &nbsp; effect&nbsp; &nbsp; &nbsp; :"slide-v",&nbsp; &nbsp; autoplay&nbsp; &nbsp; :<?php echo ($offers_animation_type): 'true'? 'false'; ?>,&nbsp; &nbsp; timer&nbsp; &nbsp; &nbsp; &nbsp;:<?php echo ($offers_animation_change): 'true'? 'false'; ?>,&nbsp; &nbsp; color&nbsp; &nbsp; &nbsp; &nbsp;:"turquoise",&nbsp; &nbsp; border&nbsp; &nbsp; &nbsp; :true});

慕盖茨4494581

&nbsp;$("#Ticker").breakingNews({&nbsp; &nbsp;effect&nbsp; &nbsp; &nbsp; :"slide-v",&nbsp; &nbsp;autoplay&nbsp; &nbsp; :<?=(isset($offers_animation_type) && !empty($offers_animation_type))? 'true' : 'false'; ?>,&nbsp; &nbsp;timer&nbsp; &nbsp; &nbsp; &nbsp;:<?=(isset($offers_animation_change) && !empty($offers_animation_change))? $offers_animation_change : 3000; ?>,&nbsp; &nbsp;color&nbsp; &nbsp; &nbsp; &nbsp;:"turquoise",&nbsp; &nbsp;border&nbsp; &nbsp; &nbsp; :true&nbsp;});
打开App,查看更多内容
随时随地看视频慕课网APP