dtiopy6088 2016-01-22 00:51
浏览 17
已采纳

JavaScript条件忽略IF

I have a conditional statement set on the url,I tested both the IF and ELSE and it does work with a simple document.write. However, I am trying to output Php i the conditional but it just always outputs the else statement, Php is not my strongest language:

<script type="text/javascript">

if(
window.location.href=="mywebsite.com"
)
{

    document.write('<label for="qty"><?php echo $this->__("Qty:") ?></label><input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" />')

}else{

    document.write('<label for="qty"><?php echo $this->__(' ') ?></label>
        <input type="hidden" name="qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" />')

}

</script>
  • 写回答

2条回答 默认 最新

  • dongzhangnong2063 2016-01-22 01:18
    关注

    window.location.href contains the full URL. If you just want to check the hostname part of the URL, use window.location.host.

    if (window.location.host == "mywebsite.com") {
        ...
    }
    

    Another option would be to do all the work on the server.

    <?php
    if ($_SERVER['SERVER_NAME'] == 'mywebsite.com') {
    ?>
    <label for="qty"><?php echo $this->__("Qty:") ?></label>
        <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" />
    <?php } else {
    ?>
    <label for="qty"><?php echo $this->__(' ') ?></label>
        <input type="hidden" name="qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" title="<?php echo $this->__("Qty") ?>" class="input-text qty" />
    <?php }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大