简体   繁体   English

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

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

im am trying to set the value for a hiddenfield with a bootstrap radio button.我正在尝试使用引导单选按钮设置隐藏字段的值。 I have an onCLick evnet on it and when it is clicked the value of the hiddenfield needs to be changed.我有一个 onCLick evnet,当它被点击时,需要更改 hiddenfield 的值。 Here is the buttons and the 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" />

and here is the script.这是脚本。

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

Do any of you have an idear why i get the error?你们中有人知道为什么我收到错误吗?

You must remove "onclick" variable, only您必须删除“onclick”变量,仅

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

and

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

try replace on尝试更换

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

I fixed it with this.我用这个修好了。

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

When the HTML was generated it changed the id of the hidden field生成 HTML 时,它更改了隐藏字段的 id

It's working fine If you just ommit onclick from function like它工作正常如果你只是从函数中省略 onclick

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

Jsfiddle提琴手

暂无
暂无

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

相关问题 未被捕获的TypeError:将innerhtml设置为数组中的字符串时,无法设置null的属性“ innerHTML” - Uncaught TypeError: Cannot set property 'innerHTML' of null- when setting the innerhtml to a string from an array 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的Typeerror:无法将属性innerHTML设置为null - Uncaught Typeerror: Cannot set property innerHTML of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法设置null的属性&#39;innerHTML&#39; - Uncaught TypeError: Cannot set property 'innerHTML' of null 错误:未捕获类型错误:无法设置 null 的属性(设置“innerHTML”) - Error: Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') 未捕获(承诺中)类型错误:无法设置 null 的属性(设置“innerHTML”) - Uncaught (in promise) TypeError: Cannot set properties of null (setting 'innerHTML') Javascript 未捕获类型错误:无法设置 null 的属性(设置“innerHTML”) - Javascript Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') Uncaught TypeError:无法在JS创建的div上将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null on JS created div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM