繁体   English   中英

未捕获的类型错误:为隐藏字段设置 innerhtml 时,无法将属性“innerHTML”设置为 null

[英]Uncaught TypeError: Cannot set property 'innerHTML' of null when setting innerhtml for hiddenfield

我正在尝试使用引导单选按钮设置隐藏字段的值。 我有一个 onCLick evnet,当它被点击时,需要更改 hiddenfield 的值。 这是按钮和隐藏字段。

<div class="myRow">
        <div class="smallCol">
            <div class="myRow">
                <div class="smallCol title">
                    <asp:Label ID="lblNewRFID" runat="server" Text="RFID etiketcode"></asp:Label>
                </div>
            </div>
            <div class="myRow">
                <div class="smallCol">
                    <div class="btn-group hiddenRFIDValue" data-toggle="buttons">
                        <label class="btn btn-default">
                            <input type="radio" class="btn" name="options" onclick="getHiddenRFIDValue('Always')" id="alwaysRFID" value="Always" autocomplete="off">
                            Always
                        </label>
                        <label class="btn btn-default">
                            <input type="radio" class="btn" name="options" onclick="getHiddenRFIDValue('Never')" id="neverRFID" value="Never" autocomplete="off">
                            Never
                        </label>
                        <label class="btn btn-default">
                            <input type="radio" class="btn" name="options" onclick="getHiddenRFIDValue('Sometimes')" id="sometimesRFID" value="Sometimes" autocomplete="off">
                            Sometimes
                        </label>
                    </div>
                </div>
            </div>
        </div>
        <asp:HiddenField ID="hiddenRFIDValue" runat="server" />

这是脚本。

<script type="text/javascript">
   onclick = function getHiddenRFIDValue(stringValue) {
       document.getElementById('hiddenRFIDValue').innerHTML = stringValue;
    }
</script>

你们中有人知道为什么我收到错误吗?

您必须删除“onclick”变量,仅

function getHiddenRFIDValue(stringValue) {
       document.getElementById('hiddenRFIDValue').innerHTML = stringValue;
    }

<asp:HiddenField ID="hiddenRFIDValue" runat="server" />

尝试更换

<input type="hidden" ID="hiddenRFIDValue" runat="server" />

我用这个修好了。

<script>
    $(".hiddenRFIDValue .btn").click(function () {
        $("#MainContent_hiddenRFIDValue").val($(this).text());
    });
</script>

生成 HTML 时,它更改了隐藏字段的 id

它工作正常如果你只是从函数中省略 onclick

function getHiddenRFIDValue(stringValue) {
   document.getElementById('hiddenRFIDValue').innerHTML = stringValue;
}

提琴手

暂无
暂无

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

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