javascript复选框document.getElementById返回null

时间:2016-08-03 12:32:31

标签: javascript checkbox

添加2个复选框后,愚蠢的小脚本无法正常工作。 目标是打开带有一些动态参数的URL。 我用谷歌搜索可能的错误,但我找不到。 DOM属性在脚本之前根据请求声明。

任何想法?谢谢

<body>
<table border="0">
    <tr>
        <td>Serial Number</td>
        <td><input type="text" id="deviceUdid" value="251528003720" maxlength="12" size="10"></td>
    </tr>
    <tr>
        <td>HouseHold</td>
        <td><input type="checkbox" id="Household" value="1"></tr>
    <tr>
        <td>Community</td>
        <td><input type="checkbox" id="Communities"></tr>
    <tr>
        <td><input type="button" id="btn" value="Submit" onClick="form_submit()"></tr>
</table>

<script type="text/javascript">
    function form_submit() {
        var a = 'https://test?';
        a += 'ACTION=authenticateUniqueDevice';
        a += '&deviceUdid=' + document.getElementById('deviceUdid').value;
        a += '&CONTENT_READ_KEY=dddddddddd';
        if (document.getElementById('HouseHold').checked) a += '&includeHousehold=true';
        if (document.getElementById('Communities').checked) a += '&includeHouseholdCommunities=true';
        window.open(a, 'MyWindow', 'width=600,height=700');

    }
</script>

1 个答案:

答案 0 :(得分:0)

更改此行(如下所示),似乎情况与HTML和脚本块之间的Household文本不同。

if (document.getElementById('Household').checked) a += '&includeHousehold=true';

修改脚本块

<script type="text/javascript">
    function form_submit() {
        var a = 'https://test?';
        a += 'ACTION=authenticateUniqueDevice';
        a += '&deviceUdid=' + document.getElementById('deviceUdid').value;
        a += '&CONTENT_READ_KEY=dddddddddd';
        if (document.getElementById('Household').checked) a += '&includeHousehold=true';
        if (document.getElementById('Communities').checked) a += '&includeHouseholdCommunities=true';
        window.open(a, 'MyWindow', 'width=600,height=700');

    }
</script>