dongpouda6700 2015-05-04 09:53 采纳率: 100%
浏览 67
已采纳

PHP Javascript无法访问php模式之外的echo元素id

Humble appologies if this is a stupid question but I cant for the life of me figure out what is wrong here.

I have an echoed element inside php mode:

echo'<select name="picks" id="winner">';
            echo'<option value="'.$row['team1'].'">'.$team1.'</option>';
            echo'<option value="'.$row['team2'].'">'.$team2.'</option>';
    echo'</select>';

Now outside php I try to do a basic javascript GET:

document.getElementById("winner");

However the elemnt is not accessible am I missing something here, is it not possible to get echoed elements Ids?

  • 写回答

5条回答 默认 最新

  • dongxi2163 2015-05-04 10:46
    关注

    You should make sure your DOM has at least loaded before performing element selects. If you can use jQuery, then this is as easy as the following, and you can place this script in the head section, or anywhere in the body:

    $(document).ready(function() {
        // Perform any selects on the DOM
    });
    

    JS

    <script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
    $(document).ready(function() {
        // The DOM is loaded and ready to be selected
        var select = document.getElementById("winner");
        var optionText = select.options[select.selectedIndex].text;
        var optionValue = select.options[select.selectedIndex].value;
    });
    </script>
    

    Of course you can also perform DOM selects using jQuery:

    <script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
    $(document).ready(function() {
        // The DOM is loaded and ready to be selected
        var optionText = $("#winner option:selected").text();
        var optionValue = $("#winner option:selected").val();
    });
    </script>
    

    Another possibility

    If $row, $team1 or $team2 are not defined and you have PHP errors and notices turned on, then the HTML will render like so:

    <select name="picks" id="winner"><b>E_NOTICE : </b> type 8 -- Undefined variable: row -- at line 4<br /><b>E_NOTICE : </b> type 8 -- Undefined variable: team1 -- at line 4<br />
        <option value=""></option><b>E_NOTICE : </b> type 8 -- Undefined variable: row -- at line 5<br /><b>E_NOTICE : </b> type 8 -- Undefined variable: team2 -- at line 5<br />
        <option value="" selected="selected"></option>
    </select>
    

    However, if you have PHP errors and warnings turned off, you would see something like this instead:

    <select name="picks" id="winner">
        <option value=""></option>
        <option value="" selected></option>
    </select>
    

    If you are unable to access the value of options in your select HTML (because they are empty), this would be a good pace to start investigating.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?