简体   繁体   English

使用 JavaScript onchange(&quot;<function> (this.id)&quot;) 在 c# CheckBox 与其他 c# 控件上

[英]The use of JavaScript onchange("<function>(this.id)") on c# CheckBox vs other c# Controls

I have a web document that has its fields populated dynamically from c# (.aspx.cs).我有一个 web 文档,它的字段从 c# (.aspx.cs) 动态填充。

Many of these fields are TextBox or HtmlTextArea elements, but some are Checkbox elements.其中许多字段是 TextBox 或 HtmlTextArea 元素,但有些是 Checkbox 元素。

For each of these I have the ID attribute populated on creation of the field, as well as using .Attributes.Add("onchange","markChanged(this.id)")对于这些中的每一个,我在创建字段时填充了 ID 属性,以及使用.Attributes.Add("onchange","markChanged(this.id)")

This works great on all the fields except Checkbox.这适用于除复选框之外的所有字段。 So I created a markCheckChange as I discovered that the Checkbox won't accept style="backgroundColor:red" or .style.backgroundColor = "red" type arguments.所以我创建了一个 markCheckChange,因为我发现 Checkbox 不接受style="backgroundColor:red".style.backgroundColor = "red"类型参数。

I also added an alert and found that the Checkbox is not actually passing the this.id into the parameter for markCheckChange(param) function.我还添加了一个警报,发现 Checkbox 实际上并没有将this.id传递到markCheckChange(param)函数的参数中。

As a result I am getting errors of the type:结果,我收到以下类型的错误:

unable to set property of undefined or null reference无法设置未定义或空引用的属性

Why and what is the difference between these controls, and is there a better way to handle this?为什么以及这些控件之间有什么区别,有没有更好的方法来处理这个问题?

I just reviewed the inspect element again, and discovered that the Checkbox control is creating more than an input field of the type checkbox, it is also wrapping it in a span tag, and the onchange function is being applied to the span tag (which has no id) and not to the input tag that has the checkbox id.我刚刚再次查看了检查元素,发现 Checkbox 控件创建的不仅仅是一个复选框类型的输入字段,它还将它包装在一个 span 标签中,并且 onchange 函数正在应用于 span 标签(它有没有 id) 而不是具有复选框 id 的输入标签。 Whereas for TextBox and HtmlTextArea the input tag is put directly within the cell/td tag, no some arbitrary span tag.而对于 TextBox 和 HtmlTextArea,输入标签直接放在 cell/td 标签中,没有一些任意的 span 标签。

So now the question becomes how to get the onchange function to apply to the input tag for the checkbox rather than the span tag encapsulating it?所以现在问题变成了如何让 onchange 函数应用于复选框的输入标签而不是封装它的 span 标签?

Per request:每个请求:

function markChange(param) {
        if (userStatus == "readonly") {
            document.getElementById("PrintRecButton").style.display = "none";
            document.getElementById("PrintPDFButton").style.display = "none";

            alert("Please login to make changes.\n\nIf you do not have access and need it,\n   contact the administrator");
            exit();
        }
        else {
            document.getElementById(param).style.backgroundColor = "teal";
            saved = false;
            var page = document.getElementById("varCurrentPage").value;
            markSaveStatus(page, false);
        }
    }

So far the markCheckChange is about the same, until I get it to pass the id correctly, I won't be able to figure out the right way to highlight the changed checkboxes.到目前为止,markCheckChange 大致相同,在我让它正确传递 id 之前,我将无法找出突出显示已更改复选框的正确方法。

I found an alternative.我找到了一个替代方案。

As I mentioned in the edit to the question, the inspect element feature revealed that the CheckBox type control was creating a set of nested elements as follows:正如我在对问题的编辑中提到的,检查元素功能显示CheckBox类型控件正在创建一组嵌套元素,如下所示:

<span onchange="markChange(this.id)">
    <input type="checkbox" id="<someValue>">
    <label for="<someValue>">
</span>

Thus when the onchange event occurred it happened at the span which has no id and thus no id was benig passed for the document.getElementById() to work.因此,当 onchange 事件发生时,它发生在没有 id 的范围内,因此没有 id 被传递给document.getElementById()工作。

While searching for why I discovered:在寻找我发现的原因时: Html 输入复选框

From there I found the following for applying labels to the checkboxes: https://stackoverflow.com/a/28675013/11035837从那里我发现以下用于将标签应用于复选框: https : //stackoverflow.com/a/28675013/11035837

So instead of using CheckBox I shall use HtmlInputCheckBox .因此,我将使用HtmlInputCheckBox而不是使用CheckBox And I have confirmed that this correctly passes the element ID to the JavaScript function.我已经确认这正确地将元素 ID 传递给了 JavaScript 函数。

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

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