简体   繁体   English

javascript复选框document.getElementById返回null

[英]javascript checkbox document.getElementById returns null

Stupid small script that does not work perfectly after I added 2 checkboxes. 添加2个复选框后,愚蠢的小脚本无法正常运行。 Objective is to open an URL with some dynamic parameters. 目的是打开带有一些动态参数的URL。 I googled to find possible error, but I don't find. 我用谷歌搜索可能的错误,但没有找到。 DOM property is declared before the script as requested. 根据要求在脚本之前声明DOM属性。

any idea ? 任何想法 ? thank you 谢谢

<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>

Change this line (as given below), Seems the case differs with your Household text between HTML and script blocks. 更改此行(如下所示),似乎情况与HTML和脚本块之间的Household文本有所不同。

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

Revised Script block 修改后的脚本块

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM