C# javascript 启用/禁用复选框
我有一些在 C# 代码隐藏中动态创建的复选框。假设有两列。这很好用。
我希望左栏中的复选框在检查时启用/禁用右侧的复选框。我通过将属性附加到运行 javascript 的代码隐藏中的复选框来执行启用/禁用操作。这很好用。
这是 javascript:
function SetPrimaryStatus(target, senders) {
var dis = "disable";
var arr = senders.split(",");
for (var i = 0; i < arr.length; i++) {
if (document.getElementById(arr[i]).checked == true) {
dis = "";
break;
}
}
document.getElementById(target).disabled = dis;
if (dis == "disable") document.getElementById(target).checked = false;
}
我以为我在家闲着……我只需在代码隐藏中创建时禁用右列复选框(CheckBox.Enabled = false;
)。好的。
现在无论出于什么原因,当我选中左侧的复选框时,右侧的复选框什么也不做。我已经在 javascript 中设置了警报,它们总是出现......并且事情似乎已按应有的方式设置。当我删除代码隐藏中的 CheckBox.Enabled = false;
时,它会再次起作用。
事实上,我可以通过选中某些左侧复选框来启动某些左侧复选框,并启用相应的右侧复选框。当我运行开始时启用的集合工作完美,但以禁用右侧复选框开始的集合则不然。
除了“启用”之外,我还应该在代码隐藏中设置其他内容,以在禁用状态下启动复选框,然后 JavaScript 可以更改该复选框吗?与“禁用”相比,这是一个真/假的问题吗?我两种都试过了。
I have some checkboxes dynamically created in C# codebehind. Let's say two columns. This works fine.
I want the checkboxes in the left column to enable/disable those in the right upon check. I do this by attaching attributes to the checkboxes in codebehind that run javascript to do the enabling/disabling. This works great.
Here's the javascript:
function SetPrimaryStatus(target, senders) {
var dis = "disable";
var arr = senders.split(",");
for (var i = 0; i < arr.length; i++) {
if (document.getElementById(arr[i]).checked == true) {
dis = "";
break;
}
}
document.getElementById(target).disabled = dis;
if (dis == "disable") document.getElementById(target).checked = false;
}
I thought I was home free...I just have to disable the right column checkboxes upon creation in the codebehind (CheckBox.Enabled = false;
). Ok.
For whatever reason now, when I check the left checkboxes, the right ones do nothing. I've set alerts in the javascript and they always come up...and things appear to be set like they should. When I remove the CheckBox.Enabled = false;
in codebehind, it works again.
In fact, I can start some of the left checkboxes with a check and the corresponding right checkbox I leave enabled. When I run the set(s) that starts enabled works perfect but the set(s) that start with the right checkbox disabled do not.
Is there something other than Enabled that I should be setting in codebehind to start the checkboxes in a disabled state that the javascript can then change? Is it a matter of true/false compared to "disable"? I've tried both.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试添加“禁用”属性。即
无论如何我认为最好使用
.removeAttribute("diabled")
方法而不是将其值设置为空字符串。try adding "disabled" attribute. i.e.
anyway I think is is better to use
.removeAttribute("diabled")
method instead of setting it's value to empty string.我在这里找到了答案: http://forums.asp.net/t/982967.aspx /1。
我必须将
document.getElementById(target).parentElement.disabled = dis;
添加到我的 javascript 函数中。现在一切都按预期进行。感谢大家的建议。I found the answer here: http://forums.asp.net/t/982967.aspx/1.
I had to add
document.getElementById(target).parentElement.disabled = dis;
to my javascript function. All works as expected now. Thanks for everyone's suggestions.