duanfu3390 2014-02-06 17:47
浏览 29
已采纳

试图比较2字符串if语句[关闭]

I am trying to compare 2 strings, but there are not comparing as I would like. I am getting a value from a div and converting it to a string, when I find the typeof it is telling me is a string but still it is passing by the if statement.

When I write.document they both look the same. Thanks

<div id="countries"><?php $geoplugin->locate(); echo"{$geoplugin->countryName}"?> </div> 

<script>
                 var country = document.getElementById("countries");
                 var count = country.innerHTML;
                 var newc = String(count);


                 if ( newc === "Ireland" ){
                    document.write("this is ireland");
                    alert("You are from Ireland");

                 }else{
                    document.write("You Live in " + newc);
                                    //alert(typeof newc);                  
                    //alert(newc);

                 }

                 </script>
  • 写回答

1条回答 默认 最新

  • drrog9853 2014-02-06 17:49
    关注

    From your HTML it looks like you've got a trailing space after the <?php ...> instruction. So whatever is there, it won't exactly equal "Ireland". Try using trim to remove any whitespace from the ends of your string.

    var newc = String(count).trim();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'